【问题标题】:How to get common array elements mongodb如何获取常见的数组元素mongodb
【发布时间】:2019-06-15 00:29:45
【问题描述】:

第一个文档:

    {
    "_id" : 1,
    "array" : [ 
        {
            "userID" : 1,
            "name" : "name 1"
        }, 
        {
            "userID" : 2,
            "name" : "name 2"
        }, 
        {
            "userID" : 3,
            "name" : "name 3"
        }, 
        {
            "userID" : 6,
            "name" : "name 6"
        }
    ]
}

第二份文件:

    {
    "_id" : 2,
    "array" : [ 
        {
            "userID" : 1,
            "name" : "name 1"
        }, 
        {
            "userID" : 2,
            "name" : "name 2"
        }, 
        {
            "userID" : 3,
            "name" : "name 3"
        }, 
        {
            "userID" : 4,
            "name" : "name 4"
        }, 
        {
            "userID" : 5,
            "name" : "name 5"
        }
    ]
}

有没有办法得到这样的输出:

   {    
    "array" : [  
        {  
            "userID" : 1,  
            "name" : "name 1" 
        },  
        {  
            "userID" : 2,  
            "name" : "name 2"  
        },   
        {  
            "userID" : 3,  
            "name" : "name 3"  
        }  
    ]  

    }  

我想比较两个文档的数组字段并检索其中的共同元素。并且仅通过常见的'userID' 比较数组字段。我们可以通过$setIntersection 来做吗?我一次只需要检查两个数组字段。

【问题讨论】:

  • 总是有 2 个文件吗?这(2个文件)是聚合结果吗?
  • 不是聚合结果。我将按document._id取两个文件
  • First DocumentSecoend Document 你的意思是,这是不同的不同对象吗?还是单个对象的一部分?
  • 你也可以看看这个答案,stackoverflow.com/questions/30646534/…
  • 是的,第一个文档和第二个文档,这是两个不同的对象

标签: mongodb spring-boot aggregation-framework


【解决方案1】:

你可以使用$setIntersection

db.t67.aggregate([
    //{$match : {}} add filter criteria to filter documents
    {$group : {_id : null, array : {$push : "$array"}}},
    {$project : {_id :0, array : {$reduce : {input : "$array", initialValue : {$arrayElemAt : ["$array", 0]}, in : {$setIntersection : ["$$this", "$$value"]}}}}}
])

输出

> db.t67.aggregate([
...     {$group : {_id : null, array : {$push : "$array"}}},
... {$project : {_id :0, array : {$reduce : {input : "$array", initialValue : {$arrayElemAt : ["$array", 0]}, in : {$setIntersection : ["$$this", "$$value"]}}}}}
... ])
{ "array" : [ { "userID" : 1, "name" : "name 1" }, { "userID" : 2, "name" : "name 2" }, { "userID" : 3, "name" : "name 3" } ] }

$unwind$group

db.t67.aggregate([
    // $match stages to filter 2 documents
    {$unwind : "$array"},
    {$group : {_id : "$array", count : {$sum : 1}}},
    {$match : {count : {$gt : 1}}},
    {$project : {count : 0}},
    {$group : {_id : null, array : {$push : "$_id"}}},
    {$project : {_id : 0}}
])

输出

{ "_id" : null, "array" : [ { "userID" : 3, "name" : "name 3" }, { "userID" : 2, "name" : "name 2" }, { "userID" : 1, "name" : "name 1" } ] }

【讨论】:

  • 感谢您的回复。我们在这里展开所有文档的数组字段,对吧?我的收藏有很多这样的文件。所以我只想拿特定的文件。不是所有的文件。我无法通过从此查询中传递文档 ID 来获取特定文档。
  • 您需要添加$match 阶段来过滤文档,在答案中我已经评论了您需要指定过滤条件的 $match 阶段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 2015-02-21
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多