<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. 如何为数组值编写匹配条件?

        时间:2023-08-30

          <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

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

                  上一篇:使用 PyMongo 的 Concat 数组因未知组运算符“$concatArrays"而失败 下一篇:使用 pygit2 拉取和集成远程更改

                  相关文章

                  <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>
                      <bdo id='mwvde'></bdo><ul id='mwvde'></ul>
                  1. <tfoot id='mwvde'></tfoot><legend id='mwvde'><style id='mwvde'><dir id='mwvde'><q id='mwvde'></q></dir></style></legend>