MongoDB多嵌套数组搜索 [英] Mongodb multi nested array search

查看:105
本文介绍了MongoDB多嵌套数组搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是搜索数据用户ID 1的记录

下面是我的数据

{ "_id" : 2,
  "name" : "test", 
  "data" :[{"_id" : "1","file" : "nic", "userid" : [1,2 ]},
           {"_id" : "2","file" : "nic1","userid" : [1 ]  },
           {"_id" : 3,"file" : "nick2","userid" : [1,2 ]} 
     ]},

{ "_id" : 3,
  "name" : "test",
  "data" : [{"_id" : "1","file" : "nic","userid" : [1,2 ]  },
            {"_id" : "2","file" : "nic1", "userid" : [3,2 ] } 
      ]}

输出应该是

{ "_id" : 2,
  "name" : "test", 
  "data" :[{"_id" : "1","file" : "nic", "userid" : [1,2 ]},
           {"_id" : "2","file" : "nic1","userid" : [1 ]  },
           {"_id" : 3,"file" : "nick2","userid" : [1,2 ]} 
     ]},

{ "_id" : 3,
  "name" : "test",
  "data" : [{"_id" : "1","file" : "nic","userid" : [1,2 ]  },          
      ]}

我尝试过

$res=$collection->find(array("data.userid" =>array('$in'=>array('52')))); 

返回空值

解决方案

您需要

 db.collection.aggregate([
    { "$match": { "data.userid": 1 } },
    { "$project": {
        "data": {
            "$setDifference": [
                { "$map": {
                    "input": "$data",
                    "as": "el",
                    "in": { 
                        "$cond": [
                            { "$setIsSubset": [ [1], "$$el.userid" ] },
                            "$$el",
                            false
                        ]
                    }
                }},
                [false]
            ]
        }
    }},
    { "$match": { "data.0": { "$exists": true } }}
])
 

对于PHP,其注释如下:

$collection->aggregate(array(
    array( '$match' => array( "data.userid" => 1 )),
    array(
        '$project' => array(
            'data' => array(
                '$setDifference' => array(
                    array(
                        '$map' => array(
                            'input' => '$data',
                            'as' => 'el',
                            'in' => array(
                                '$cond' => array(
                                    array( '$setIsSubset' => array(array(1),'$$el.userid') ),
                                    '$$el',
                                    FALSE
                                )
                            )
                        )
                    ),
                    array(FALSE)
                )
            )
        )
    ),
    array( '$match' => array( 'data.0' => array( '$exists' => TRUE ) ) )
))

$map 运算符允许检查外部数组的每个元素,并将每个元素传递给 $cond 三元运算.这将处理 $setIsSubset 操作在内部"数组上查看它是否实际上包含备用集中的值之一(在本例中为[1]),并在其中进行true评估的情况下返回该元素,否则返回false. /p>

$setDifference 的要点

a>是从修改后的数组中删除那些false值,并且仅返回匹配的元素.最后, $exists 测试看起来是外部数组实际上至少有一个元素,并且由于过滤而不能为空.

返回的文档是具有匹配条件的文档,并且仅是也符合指定条件的数组元素.

当然,这里的操作员要求您至少拥有MongoDB 2.6作为服务器(现在是一个很旧的版本,至少是一个建议的更新),但是如果您的版本仍然较小,则需要使用 $unwind

My aim is to search records of data userid 1

Below is my data

{ "_id" : 2,
  "name" : "test", 
  "data" :[{"_id" : "1","file" : "nic", "userid" : [1,2 ]},
           {"_id" : "2","file" : "nic1","userid" : [1 ]  },
           {"_id" : 3,"file" : "nick2","userid" : [1,2 ]} 
     ]},

{ "_id" : 3,
  "name" : "test",
  "data" : [{"_id" : "1","file" : "nic","userid" : [1,2 ]  },
            {"_id" : "2","file" : "nic1", "userid" : [3,2 ] } 
      ]}

out put should be

{ "_id" : 2,
  "name" : "test", 
  "data" :[{"_id" : "1","file" : "nic", "userid" : [1,2 ]},
           {"_id" : "2","file" : "nic1","userid" : [1 ]  },
           {"_id" : 3,"file" : "nick2","userid" : [1,2 ]} 
     ]},

{ "_id" : 3,
  "name" : "test",
  "data" : [{"_id" : "1","file" : "nic","userid" : [1,2 ]  },          
      ]}

I tried

$res=$collection->find(array("data.userid" =>array('$in'=>array('52')))); 

returning null

解决方案

You need the .aggregate() method in order to "filter" any array content for more than a singular match, and also the basic match is a lot simplier as MongoDB does not care that the data is within arrays, just as long as the specified path is correct:

db.collection.aggregate([
    { "$match": { "data.userid": 1 } },
    { "$project": {
        "data": {
            "$setDifference": [
                { "$map": {
                    "input": "$data",
                    "as": "el",
                    "in": { 
                        "$cond": [
                            { "$setIsSubset": [ [1], "$$el.userid" ] },
                            "$$el",
                            false
                        ]
                    }
                }},
                [false]
            ]
        }
    }},
    { "$match": { "data.0": { "$exists": true } }}
])

With PHP this notates as follows:

$collection->aggregate(array(
    array( '$match' => array( "data.userid" => 1 )),
    array(
        '$project' => array(
            'data' => array(
                '$setDifference' => array(
                    array(
                        '$map' => array(
                            'input' => '$data',
                            'as' => 'el',
                            'in' => array(
                                '$cond' => array(
                                    array( '$setIsSubset' => array(array(1),'$$el.userid') ),
                                    '$$el',
                                    FALSE
                                )
                            )
                        )
                    ),
                    array(FALSE)
                )
            )
        )
    ),
    array( '$match' => array( 'data.0' => array( '$exists' => TRUE ) ) )
))

The $map operator allows inspection of each element ofthe outer array and passed each element to the $cond ternary operation. This processes a $setIsSubset operation on the "inner" array to see if it in fact contains one of the values in the alternate set ( in this case [1] ) and where a true evaluation is made then the element is returned or otherwise false.

The point of $setDifference is to remove those false values from the modified array and only return matched elements. And finally the $exists test looks to see that outer array actually has at least one element and is not empty as a result of the filtering.

The documents returned are the ones with the matching condition and only the array elements that also match the specified condition.

Of course the operators here require that you have at least MongoDB 2.6 as the server ( which is quite an old release now and an advised update at least ) but if you still have a lesser version then you need a traditional approach with $unwind and $group:

$collection->aggregate(array(
    array( '$match' => array( "data.userid" => 1 )),
    array( '$unwind' => '$data' ),
    array( '$match' => array( 'data.userid' => 1 )),
    array( 
        '$group' => array(
            '_id' => '$_id',
            'data' => array( '$push' => '$data' )
        )
    )
))

这篇关于MongoDB多嵌套数组搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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