当元素中的一个key:value等于我要寻找的内容时,如何查询mongodb以获取数组的元素

时间:2019-05-03 01:05:01

标签: python mongodb

我正在寻找一种查询mongoDB的方法,以使数组中的元素仅等于我要寻找的元素。可以说我的收藏夹里有这个东西:

{
  "_id": {
    "$oid": "5cc8ec4efb6fc00ed59ea5fd"
  },
  "list": [
    {
      "index": 1,
      "payments": [
        {
          "sender": "Mark",
          "receiver": "Marie",
          "amount": 150
        },
        {
          "sender": "Sarah",
          "receiver": "Tom",
          "amount": 180
        }
      ]
    },
    {
      "index": 2,
      "payments": [
        {
          "sender": "Oli",
          "receiver": "Eli",
          "amount": 120
        },
        {
          "sender": "Mark",
          "receiver": "Tommy",
          "amount": 160
        }
      ]
    }
  ]
}

我想返回“列表”内的所有元素,其中“发送者”等于“标记”或“接收者”等于“ Eli”

我尝试使用$ elemMatch,但是当它不返回任何东西时,它正在返回整个对象。

这是我的数千个测试之一,它将返回整个结果。

 def get_my_tx(account_name, account_password):
   keys = get_keys(account_name, account_password)
   db = connect_to_db_blockchain()
   query = {
     "$or": [{
       "list.payments.sender": "Mark"
     }, {
       "list.payments.receiver": "Eli"
     }]
   }
   projection = {
     "list.payments.sender": 1,
     "list.payments.receiver": 1,
     "list.payments.amount": 1,
     "_id": 0
   }
   my_tx = db.find(query, projection=projection)
   return my_tx

0 个答案:

没有答案