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

  • <tfoot id='hBTTc'></tfoot>

      1. <small id='hBTTc'></small><noframes id='hBTTc'>

          <bdo id='hBTTc'></bdo><ul id='hBTTc'></ul>
        <legend id='hBTTc'><style id='hBTTc'><dir id='hBTTc'><q id='hBTTc'></q></dir></style></legend>
      2. 如何为数组值编写匹配条件?

        How to write match condition for array values?(如何为数组值编写匹配条件?)

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

              <legend id='UL9t7'><style id='UL9t7'><dir id='UL9t7'><q id='UL9t7'></q></dir></style></legend>
            1. <tfoot id='UL9t7'></tfoot>
                  <tbody id='UL9t7'></tbody>
              1. <small id='UL9t7'></small><noframes id='UL9t7'>

                • 本文介绍了如何为数组值编写匹配条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已将值存储在多个变量中.下面是输入变量.

                  I have stored values in multiple variables. below are the input variables.

                  uid = Objectid("5d518caed55bc00001d235c1")
                  disuid = ['5d76b2c847c8d3000184a090', '5d7abb7a97a90b0001326010']
                  

                  这些值是动态更改的.以下是我的代码:

                  These values are changed dynamically. and below is my code:

                  user_posts.aggregate([{
                      "$match": {
                          "$or": [{ "userid": uid }, {
                              "userid": {
                                  "$eq":
                                      disuid
                              }
                          }]
                      }
                  },
                  {
                      "$lookup": {
                          "from": "user_profile",
                          "localField": "userid",
                          "foreignField": "_id",
                          "as": "details"
                      }
                  },
                  { "$unwind": "$details" },
                  {
                      "$sort": { "created_ts": -1 }
                  },
                  {
                      "$project": {
                          "userid": 1,
                          "type": 1,
                          "location": 1,
                          "caption": 1
                      }
                  }
                  ])
                  

                  在上面的代码中,我只获得了匹配的 uid 值,但我还需要匹配到 disuid 的文档.

                  In the above code, I am getting matched uid values only but I need documents matched to disuid also.

                  userid 字段中,我们仅存储了Objectid"值.所以我关心的是如何将Objectid"添加到disuid"变量以及如何使用 userid 字段为这两个变量编写匹配条件?

                  In userid field, we have stored "Objectid" values only. So my concern is how to add "Objectid" to "disuid" variable and how to write match condition for both variables using userid field?

                  推荐答案

                  好的,有两种方法:

                  就像你一样:

                  uid = Objectid("5d518caed55bc00001d235c1")
                  disuid = ['5d76b2c847c8d3000184a090', '5d7abb7a97a90b0001326010']
                  

                  您需要使用 python 代码将字符串列表转换为 ObjectId 列表:

                  You need to convert your list of strings to list of ObjectId's using python code :

                  from bson.objectid import ObjectId
                  
                  
                  disuid = ['5d76b2c847c8d3000184a090', '5d7abb7a97a90b0001326010']
                  my_list = []
                  
                  
                  for i in disuid:
                      my_list.append(ObjectId(i))
                  

                  它看起来像这样:[ObjectId('5d76b2c847c8d3000184a090'),ObjectId('5d7abb7a97a90b0001326010')]

                  然后通过使用新列表my_list,您可以像这样进行查询:

                  then by using new list my_list, you can do query like this :

                  user_posts.aggregate([{"$match" : { "$or" : [{ "userid" : uid }, { "userid" : { "$in" : my_list }}]}}])
                  

                  或者以我不喜欢的其他方式,因为与数据库中所有文档的 userid 字段的 n 个值相比,只转换少数代码更容易,但以防万一你希望它使用数据库查询来完成:

                  Or in the other way which I wouldn't prefer, as converting just few in code is easier compared to n num of values for userid field over all documents in DB, but just in case if you want it to be done using DB query :

                  user_posts.aggregate([{$addFields : {userStrings : {$toString: '$userid'}}},{"$match" : { "$or" : [{ "userid" : uid }, { "userStrings" : { "$in" : disuid }}]}}])
                  

                  注意:如果你没有 bson 包,那么你需要通过 pip install bson

                  Note : In case if you don't have bson package, then you need to install it by doing something like pip install bson

                  这篇关于如何为数组值编写匹配条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

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

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