比较数组并返回差异 [英] Compare arrays and Return the Difference

查看:15
本文介绍了比较数组并返回差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时在内存中创建了一个数组 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].

数组B,存储在mongodb中,为[1, 3, 5, 6, 7, 10].

Array B, stored in mongodb, is [1, 3, 5, 6, 7, 10].

我期望的结果是 [2, 4].

推荐答案

响应中修改"文档的唯一内容是 .aggregate().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] 这样的数组,那么您可以与 $filter$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和前面的 $anyElementTrue版本:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆