【问题标题】:query mongodb inside nested array of objects在嵌套的对象数组中查询 mongodb
【发布时间】:2018-11-01 11:22:58
【问题描述】:

我在一个这样的集合中有条目:

{
   "overlaps": [
      {"BB1": "itemA", "iou": 0.1, "BB2": "itemB"},
      {"BB1": "itemB", "iou": 0.45, "BB2": "itemC"}
   ],
   "elemID": 1,
   "otherfield2": "whateverelse"
}

我想在重叠数组中找到具有重叠元素的条目,其中重叠。BB1:“itemA”和重叠。BB2:“itemC”。但是对于重叠数组中的相同元素。

例如,这里给出的示例不应该被检索,因为我有overlaps.BB1:"itemA"和overlaps.BB2:"itemC",但不在同一个元素中。

一个有效的元素是:

{
   "overlaps": [
      {"BB1": "itemA", "iou": 0.1, "BB2": "itemC"},
      {"BB1": "itemB", "iou": 0.45, "BB2": "itemC"}
   ],
   "elemID": 2,
   "otherfield2": "whateverelse"
}

我试过了,但是不行

cursor = record1.find({"$and": [{"overlaps.BB1":"itemA"},{"overlaps.BB2":"itemC"}]}) 

我怎样才能做到这一点?或者我应该改变我的数据结构以便能够执行这样的查询?

谢谢

【问题讨论】:

    标签: json mongodb mongodb-query pymongo


    【解决方案1】:

    您应该使用$elemMatch 来获取结果

    db.collection.find({
      overlaps: {
        $elemMatch: {
          BB1: "itemA",
          BB2: "itemC"
        }
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2012-07-23
      • 2016-09-05
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多