比较数组并返回差值 [英] Compare arrays and Return the Difference
问题描述
我在运行时在内存中创建了一个数组A,另一个数组B保存在mongo数据库中.如何有效地从A中获得所有不在B中的元素?
I have an array A in memory created at runtime and another array B saved in a mongo database. How can I efficiently get all the elements from A that are not in B?
您可以假设存储在mongodb中的数组比运行时创建的数组大几个数量级,因此,我认为从mongo获取完整数组并计算结果效率不高,但是我没有在mongo中发现了任何查询操作,可以让我计算出想要的结果.
You can assume that the array stored in mongodb is several orders of magnitude bigger than the array created at runtime, for that reason I think that obtaining the full array from mongo and computing the result would not be efficient, but I have not found any query operation in mongo that allows me to compute the result I want.
请注意, $ nin 运算符的作用与之相反.我想要的,即它从B中检索不在A中的元素.
Note that the $nin operator does the opposite of what I want, i.e., it retrieves the elements from B that are not in A.
示例:
在运行时在我的应用中创建的数组A为[2, 3, 4]
.
Array A, created in my appliction at runtime, is [2, 3, 4]
.
存储在mongodb中的数组B是[1, 3, 5, 6, 7, 10]
.
Array B, stored in mongodb, is [1, 3, 5, 6, 7, 10]
.
我期望的结果是[2, 4]
.
推荐答案
在响应中修改"文档的唯一内容是 .mapReduce()
,其中前者是更好的选择.
The only things that "modify" the document in response are .aggregate()
and .mapReduce()
, where the former is the better option.
在这种情况下,您需要的 $setDifference
比较集合"并返回两者之间的差异".
In that case you are asking for $setDifference
which compares the "sets" and returns the "difference" between the two.
因此用数组表示一个文档:
So representing a document with your array:
db.collection.insert({ "b": [1, 3, 5, 6, 7, 10] })
运行聚合:
db.collection.aggregate([{ "$project": { "c": { "$setDifference": [ [2,3,4], "$b" ] } } }])
哪个返回:
{ "_id" : ObjectId("596005eace45be96e2cb221b"), "c" : [ 2, 4 ] }
如果您不希望集合",而是想提供像[2,3,4,4]
这样的数组,则可以与 $in
,如果您至少具有MongoDB 3.4:
If you do not want "sets" and instead want to supply an array like [2,3,4,4]
then you can compare with $filter
and $in
instead, if you have MongoDB 3.4 at least:
db.collection.aggregate([
{ "$project": {
"c": {
"$filter": {
"input": [2,3,4,4],
"as": "a",
"cond": {
"$not": { "$in": [ "$$a", "$b" ] }
}
}
}
}}
])
或使用 $filter
和
Or with $filter
and $anyElementTrue
in earlier versions:
db.collection.aggregate([
{ "$project": {
"c": {
"$filter": {
"input": [2,3,4,4],
"as": "a",
"cond": {
"$not": {
"$anyElementTrue": {
"$map": {
"input": "$b",
"as": "b",
"in": {
"$eq": [ "$$a", "$$b" ]
}
}
}
}
}
}
}
}}
])
两者都会返回:
{ "_id" : ObjectId("596005eace45be96e2cb221b"), "c" : [ 2, 4, 4 ] }
由于4
作为输入两次"提供,因此当然不是一组",因此也返回两次".
Which is of course "not a set" since the 4
was provided as input "twice" and is therefore returned "twice" as well.
这篇关于比较数组并返回差值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!