猫鼬从传递的数组中找到不匹配的ID [英] Mongoose find not match ids from array which passed
本文介绍了猫鼬从传递的数组中找到不匹配的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被困在猫鼬查询中.我有一个ID数组作为输入,我想在数据库中搜索该ID数组以查找匹配的元素.在find
查询中使用$in
后,它将返回完美结果.
I'm stuck in mongoose query. I've an array of ids as input, I want to search that array of ids in a database for matching elements. It returns perfect result after using $in
in find
query.
现在,我希望该ID来自数据库中找不到的数组.最好的方法是什么?
Now, I want that Ids from an array which is not found in the database. what is the best way to do it?
请尝试对其进行评论.
Template.find({
_ids : [
"as6d87as67da7s8d87a87", // available in database
"as6dasd8sa9d8a9a9s8d7", // not-available in database
"6756asd5as6dsadghasd3", // available in database
]
}, function(err, result){
// Need result as "as6dasd8sa9d8a9a9s8d7", which is not matched. or in object.
// Can we do with `aggregate` query?
});
推荐答案
我认为这满足了您的需求
I think this makes what you want
var _ids = [
"as6d87as67da7s8d87a87",
"as6dasd8sa9d8a9a9s8d7",
"6756asd5as6dsadghasd3"
];
Template.find({
_ids : _ids
}, function(err, result){
var filteredResult = _ids.filter(currentId => !result.some(item => item._id.toString() == currentId));
});
这篇关于猫鼬从传递的数组中找到不匹配的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文