Node.js 如何从对象Mongodb下的数组中删除对象

Node.js 如何从对象Mongodb下的数组中删除对象,node.js,mongodb,Node.js,Mongodb,我有这样一个数据集: { "_id" : ObjectId("5bacc98431481e0520856df8"), "action" : { "count" : 0, "shop" : [ { "uid" : ObjectId("5c0b396a50b8a627c4172a2b"), }, { "uid"

我有这样一个数据集:

{
    "_id" : ObjectId("5bacc98431481e0520856df8"),
    "action" : {
        "count" : 0,
        "shop" : [ 
            {
                "uid" : ObjectId("5c0b396a50b8a627c4172a2b"),

            }, 
            {
                "uid" : ObjectId("5c0b3b471ed11f2124e1e3a8"),

            },
            {
                "uid" : ObjectId("5c0b3b471ed11f2124e1e3a9"),

            }
         ]
      }
}
db.CollectionName.find({_id: ObjectId("5bacc98431481e0520856df8")})
.then(data => {
  if (data.length > 0) {
        let xData = data[0].notifications.shop;
        let xuid = ObjectId("5c0b3b471ed11f2124e1e3a8");
        let filterData = xData.filter(
          x => x.uid!= xuid
        );
        User.update(
          { _id: ObjectId("5bacc98431481e0520856df8")},
          {
            $set: {
              action: { shop: filterData }
            }
          }
        )
        .then(usr => {
          console.log("deleted successfully")
         })
         .catch(er => {
           console.log(er)
         })
  }
})
.catch(error => {
    console.log(error)
}) 
如何通过mongodb查询删除上面的对象,该对象的
uid
ObjectId(“5c0b3b471ed11f2124e1e3a8”)

我使用的方法在我看来并不完美。我的方法是这样的:

{
    "_id" : ObjectId("5bacc98431481e0520856df8"),
    "action" : {
        "count" : 0,
        "shop" : [ 
            {
                "uid" : ObjectId("5c0b396a50b8a627c4172a2b"),

            }, 
            {
                "uid" : ObjectId("5c0b3b471ed11f2124e1e3a8"),

            },
            {
                "uid" : ObjectId("5c0b3b471ed11f2124e1e3a9"),

            }
         ]
      }
}
db.CollectionName.find({_id: ObjectId("5bacc98431481e0520856df8")})
.then(data => {
  if (data.length > 0) {
        let xData = data[0].notifications.shop;
        let xuid = ObjectId("5c0b3b471ed11f2124e1e3a8");
        let filterData = xData.filter(
          x => x.uid!= xuid
        );
        User.update(
          { _id: ObjectId("5bacc98431481e0520856df8")},
          {
            $set: {
              action: { shop: filterData }
            }
          }
        )
        .then(usr => {
          console.log("deleted successfully")
         })
         .catch(er => {
           console.log(er)
         })
  }
})
.catch(error => {
    console.log(error)
}) 
通过这种方法,我将
uid
从对象下的数组中删除。如果有人知道这类任务是通过MongoDB查询完成的,请告诉我


非常感谢您的帮助/建议。提前感谢尝试我的查询的开发人员。

执行此操作的mongo语句是:

db.getCollection('YOURCOLECTION').update(
{'_id': ObjectId("5bacc98431481e0520856df8")}, 
{ "$pull": 
       { "action.shop": { "uid": ObjectId("5c0b3b471ed11f2124e1e3a8") }} 
})

您可以将命令与
$update

结合使用,尝试使用这个
db.collection.findOneAndUpdate({“\u id”:ObjectId('5bacc98431481e0520856df8'),{“$pull:{”action.$.shop:{“uid”:ObjectId('5c0b371ed11f2124e1e3a8')})
dear@AnthonyWinzlet我跟踪了您的查询,得到的错误是MongoError:位置运算符没有从QueryDar中找到所需的匹配项,我跟踪了您的查询,根据这一点,数组中没有任何内容。我得到的mongodb响应是:{n:1,nModified:0,opTime:{ts:Timestamp{bsontype:'Timestamp',low:2,high:'1544246568},t:374},electionId:7fffff000000000000176,ok:1,operationTime:Timestamp{bsontype:'Timestamp',low:2,high:'1544246568},$clusterTime',clusterTime:{clusterTime:Timestamp{bsontype:'Timestamp',low:2,high:1544246568},signature:{hash:[Object],keyId:0}}}}这里nModified是0,1我在mongoDB 3.6上用您的确切数据对它进行了本地测试,所以我非常确定查询是可靠的。确保在
db.getCollection('yourcollection'中有正确的
集合
collection
etcYes您是对的,实际上我犯了一个mistypo错误,这就是原因,无论如何,谢谢,我接受了答案