Node.js 使用Nodejs从MongoDB中删除项
我正在尝试从所有引用它的集合中删除用户id。我将从表单中引入一个用户id,并希望删除每个业务集合中对它的所有引用。我知道下面的查询不起作用,但它显示了我当前的方法Node.js 使用Nodejs从MongoDB中删除项,node.js,mongodb,Node.js,Mongodb,我正在尝试从所有引用它的集合中删除用户id。我将从表单中引入一个用户id,并希望删除每个业务集合中对它的所有引用。我知道下面的查询不起作用,但它显示了我当前的方法 db.collection('business', function (err, allBus){ allBus.update({}, { $pull: {followers: { userID } } } ); }); 这是我的数据,有什么想法吗 { "_id" : ObjectId("55355d0ab06370
db.collection('business', function (err, allBus){
allBus.update({}, { $pull: {followers: { userID } } } );
});
这是我的数据,有什么想法吗
{
"_id" : ObjectId("55355d0ab063708c0b73809e"),
"address" : "Donegal",
"businessName" : "burkes shoes",
"email" : "info@burkes.ie",
"followers" : [
ObjectId("55300f5208224af428d1beaf"),
ObjectId("553129666252d2fc0a4634e4")
],
"gpsLat" : "55.1763595",
"gpsLong" : "-7.7923",
"homeDomain" : "www.burkes.ie",
"imgpath" : "\\images\\uploads\\57461Burkes_logo_1429560586607.jpg",
"password" : "1",
"role" : "business"
}
如果
userID
是一个字符串,则需要先将其转换为ObjectID,然后再在查询中使用它。像这样的东西应该会起到神奇的作用:
var ObjectID = require("mongodb").ObjectID,
userID = new ObjectId("55300f5208224af428d1beaf");
/*
if userID is a string then this will work
var userID = new ObjectId(userID);
*/
db.business.update(
{"followers": userID},
{
"$pull": { "followers": userID }
},
{ multi: true }
);
上面的查询将比没有查询的更新具有更好的性能,因为它首先过滤追随者数组中具有
userID
值的元素的文档,然后通过从数组中提取ObjectID
值来更新匹配的文档 您想删除整个记录还是只从“追随者”
中删除数据?还要匹配“\u id”
字段吗?我只想从我不清楚的每个实例中删除一个id(表单中的userID)。如果id='55355d0ab063708c0b73809e'
则将从作为示例提供的数据中删除哪些内容。同样对于此值55300f520824af428d1beaf
。是否要删除userID
包含在followers
中的那些文档?是的,没错。