<i id='18vaM'><tr id='18vaM'><dt id='18vaM'><q id='18vaM'><span id='18vaM'><b id='18vaM'><form id='18vaM'><ins id='18vaM'></ins><ul id='18vaM'></ul><sub id='18vaM'></sub></form><legend id='18vaM'></legend><bdo id='18vaM'><pre id='18vaM'><center id='18vaM'></center></pre></bdo></b><th id='18vaM'></th></span></q></dt></tr></i><div id='18vaM'><tfoot id='18vaM'></tfoot><dl id='18vaM'><fieldset id='18vaM'></fieldset></dl></div>
  • <small id='18vaM'></small><noframes id='18vaM'>

      <bdo id='18vaM'></bdo><ul id='18vaM'></ul>
    <legend id='18vaM'><style id='18vaM'><dir id='18vaM'><q id='18vaM'></q></dir></style></legend>

      <tfoot id='18vaM'></tfoot>

      1. Mongodb 多嵌套数组搜索

        Mongodb multi nested array search(Mongodb 多嵌套数组搜索)

          <bdo id='1vJYd'></bdo><ul id='1vJYd'></ul>
          • <small id='1vJYd'></small><noframes id='1vJYd'>

          • <tfoot id='1vJYd'></tfoot>

                1. <i id='1vJYd'><tr id='1vJYd'><dt id='1vJYd'><q id='1vJYd'><span id='1vJYd'><b id='1vJYd'><form id='1vJYd'><ins id='1vJYd'></ins><ul id='1vJYd'></ul><sub id='1vJYd'></sub></form><legend id='1vJYd'></legend><bdo id='1vJYd'><pre id='1vJYd'><center id='1vJYd'></center></pre></bdo></b><th id='1vJYd'></th></span></q></dt></tr></i><div id='1vJYd'><tfoot id='1vJYd'></tfoot><dl id='1vJYd'><fieldset id='1vJYd'></fieldset></dl></div>
                  <legend id='1vJYd'><style id='1vJYd'><dir id='1vJYd'><q id='1vJYd'></q></dir></style></legend>

                    <tbody id='1vJYd'></tbody>
                  本文介绍了Mongodb 多嵌套数组搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的目的是搜索数据userid 1的记录

                  My aim is to search records of data userid 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')))); 
                  

                  返回空

                  推荐答案

                  您需要 .aggregate() 方法以过滤"任何数组内容以进行多个单一匹配,而且基本匹配要简单得多,因为 MongoDB 并不关心数据在数组内,只要指定的路径是正确的:

                  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 } }}
                  ])
                  

                  在 PHP 中,这表示如下:

                  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 ) ) )
                  ))
                  

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

                  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.

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

                  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.

                  当然,这里的操作员要求您至少有 MongoDB 2.6 作为服务器(这是一个相当老的版本,至少是一个建议的更新)但是如果您仍然有一个较小的版本,那么您需要使用 $unwind$group:

                  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 多嵌套数组搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  • <bdo id='TEPX5'></bdo><ul id='TEPX5'></ul>
                        <tbody id='TEPX5'></tbody>
                        • <tfoot id='TEPX5'></tfoot>
                          <i id='TEPX5'><tr id='TEPX5'><dt id='TEPX5'><q id='TEPX5'><span id='TEPX5'><b id='TEPX5'><form id='TEPX5'><ins id='TEPX5'></ins><ul id='TEPX5'></ul><sub id='TEPX5'></sub></form><legend id='TEPX5'></legend><bdo id='TEPX5'><pre id='TEPX5'><center id='TEPX5'></center></pre></bdo></b><th id='TEPX5'></th></span></q></dt></tr></i><div id='TEPX5'><tfoot id='TEPX5'></tfoot><dl id='TEPX5'><fieldset id='TEPX5'></fieldset></dl></div>

                          <legend id='TEPX5'><style id='TEPX5'><dir id='TEPX5'><q id='TEPX5'></q></dir></style></legend>

                            <small id='TEPX5'></small><noframes id='TEPX5'>