给定一个 ID 列表,查询集合中不存在哪些 ID 的最佳方法是什么?
Given a list of ids, what's the best way to query which ids do not exist in the collection?
我有一组包含唯一 ID 字段的文档。现在我有一个 id 列表,其中可能包含一些不存在于集合中的 id。从列表中找出这些 ID 的最佳方法是什么?
我知道我可以使用 $in 运算符来获取列表中包含 id 的文档,然后与给定的 id 列表进行比较,但是有更好的方法吗?
不幸的是 MongoDB 只能使用内置函数(否则我建议使用 set
),但您可以尝试在列表中找到所有不同的 ID,然后手动将它们拉出来。
类似于(未测试):
var your_unique_ids = ["present", "not_present"];
var present_ids = db.getCollection('your_col').distinct('unique_field', {unique_field: {$in: your_unique_ids}});
for (var i=0; i < your_unique_ids.length; i++) {
var some_id = your_unique_ids[i];
if (present_ids.indexOf(some_id) < 0) {
print(some_id);
}
}
我想你的 collection 中有以下文件:
{ "_id" : ObjectId("55b725fd7279ca22edb618bb"), "id" : 1 }
{ "_id" : ObjectId("55b725fd7279ca22edb618bc"), "id" : 2 }
{ "_id" : ObjectId("55b725fd7279ca22edb618bd"), "id" : 3 }
{ "_id" : ObjectId("55b725fd7279ca22edb618be"), "id" : 4 }
{ "_id" : ObjectId("55b725fd7279ca22edb618bf"), "id" : 5 }
{ "_id" : ObjectId("55b725fd7279ca22edb618c0"), "id" : 6 }
和以下列表id
var listId = [ 1, 3, 7, 9, 8, 35 ];
我们可以使用 .filter
方法来 return 不在你的 collection.
中的 ids
数组
var result = listId.filter(function(el){
return db.collection.distinct('id').indexOf(el) == -1; });
这会产生
[ 7, 9, 8, 35 ]
现在您还可以使用 aggregation frameworks and the $setDifference
运算符。
db.collection.aggregate([
{ "$group": { "_id": null, "ids": { "$addToSet": "$id" }}},
{ "$project" : { "missingIds": { "$setDifference": [ listId, "$ids" ]}, "_id": 0 }}
])
这产生:
{ "missingIds" : [ 7, 9, 8, 35 ] }
下面的查询将为您获取结果:
var listid = [1,2,3,4];
db.collection.aggregate([
{$project: { uniqueId :
{
"$setDifference":
[ listid , db.collection.distinct( "unique_field" )]} , _id : 0 }
},
{$limit:1}
]);
我有一组包含唯一 ID 字段的文档。现在我有一个 id 列表,其中可能包含一些不存在于集合中的 id。从列表中找出这些 ID 的最佳方法是什么?
我知道我可以使用 $in 运算符来获取列表中包含 id 的文档,然后与给定的 id 列表进行比较,但是有更好的方法吗?
不幸的是 MongoDB 只能使用内置函数(否则我建议使用 set
),但您可以尝试在列表中找到所有不同的 ID,然后手动将它们拉出来。
类似于(未测试):
var your_unique_ids = ["present", "not_present"];
var present_ids = db.getCollection('your_col').distinct('unique_field', {unique_field: {$in: your_unique_ids}});
for (var i=0; i < your_unique_ids.length; i++) {
var some_id = your_unique_ids[i];
if (present_ids.indexOf(some_id) < 0) {
print(some_id);
}
}
我想你的 collection 中有以下文件:
{ "_id" : ObjectId("55b725fd7279ca22edb618bb"), "id" : 1 }
{ "_id" : ObjectId("55b725fd7279ca22edb618bc"), "id" : 2 }
{ "_id" : ObjectId("55b725fd7279ca22edb618bd"), "id" : 3 }
{ "_id" : ObjectId("55b725fd7279ca22edb618be"), "id" : 4 }
{ "_id" : ObjectId("55b725fd7279ca22edb618bf"), "id" : 5 }
{ "_id" : ObjectId("55b725fd7279ca22edb618c0"), "id" : 6 }
和以下列表id
var listId = [ 1, 3, 7, 9, 8, 35 ];
我们可以使用 .filter
方法来 return 不在你的 collection.
ids
数组
var result = listId.filter(function(el){
return db.collection.distinct('id').indexOf(el) == -1; });
这会产生
[ 7, 9, 8, 35 ]
现在您还可以使用 aggregation frameworks and the $setDifference
运算符。
db.collection.aggregate([
{ "$group": { "_id": null, "ids": { "$addToSet": "$id" }}},
{ "$project" : { "missingIds": { "$setDifference": [ listId, "$ids" ]}, "_id": 0 }}
])
这产生:
{ "missingIds" : [ 7, 9, 8, 35 ] }
下面的查询将为您获取结果:
var listid = [1,2,3,4];
db.collection.aggregate([
{$project: { uniqueId :
{
"$setDifference":
[ listid , db.collection.distinct( "unique_field" )]} , _id : 0 }
},
{$limit:1}
]);