Mongodb 如果在不同的集合中,则从结果中删除文档
我有分配、签入和资产集合,当我进入Mongodb 如果在不同的集合中,则从结果中删除文档,mongodb,mongoose,Mongodb,Mongoose,我有分配、签入和资产集合,当我进入/assign页面时,我运行以下查询: Asset.find({"author.id":req.user.id}, function(err, allAsset) { if(err){ console.log(err); } else { res.locals.assets = allAsset; // Set the data in locals next();
/assign
页面时,我运行以下查询:
Asset.find({"author.id":req.user.id}, function(err, allAsset) {
if(err){
console.log(err);
} else {
res.locals.assets = allAsset; // Set the data in locals
next();
}
});
查找登录用户拥有的所有资产。我希望这样,如果已分配资产
,则该资产将无法再次分配。mongodb中指定资产的示例文档为:
{ "_id" : ObjectId("59fb2cdb02b2a83502cd0df6"),
"subuser" : "Kirbytech",
"checkoutDate" : "2017-12-30",
"notes" : "241361",
"author" : {
"id" : ObjectId("59d7f98a77fcc221d6e3c93d"),
"email" : "joe@example.com.ca"
}, "asset" : {
"id" : "59f7cde58a6f9b11e1cd07d9",
"num" : 1005
},
"__v" : 0 }
我看到了,但这对我帮助不大。我知道我需要搜索找到或未找到asset.id
的所有文档(不确定是哪个),然后将这些结果保存到res.locals.assets
我当然希望这是有意义的。如果需要,请要求澄清 是否可以使用用户id和资产id查询Assign集合,如果返回值为null,则表示尚未分配资产 我假设分配的记录是在资产“分配”给用户时创建的
Assign.find({}, function(err, assignedRecords){
// this will be an array of all assigned assets
let allAssignedAssets = assignedRecords.map((doc) =>doc.asset.id)
// now get all the assets not in the above array
Asset.find({id:{$nin: allAssignedAssets}}, function(err, allUnassignedAssets) {
if(err){
console.log(err);
} else {
res.locals.assets = allUnassignedAssets; // all unassigned assets
next();
}
}
})
如果您需要更多说明,请告诉我。您是否可以使用用户id和资产id查询分配集合,如果返回值为空,则资产尚未分配 我假设分配的记录是在资产“分配”给用户时创建的
Assign.find({}, function(err, assignedRecords){
// this will be an array of all assigned assets
let allAssignedAssets = assignedRecords.map((doc) =>doc.asset.id)
// now get all the assets not in the above array
Asset.find({id:{$nin: allAssignedAssets}}, function(err, allUnassignedAssets) {
if(err){
console.log(err);
} else {
res.locals.assets = allUnassignedAssets; // all unassigned assets
next();
}
}
})
如果你需要更多的澄清,请告诉我