如何在MongoDB中使用多个条件查询数组中的对象?

时间:2016-05-18 04:14:51

标签: mongodb mongodb-query

这是我的数据

// Data 1
{
  name : 111, 
  factors : 
  [
    {name:"f1", value:"dog", unit : "kg"},
    {name:"f2", value:"0"},
    {name: "f3", value:"rain"}
  ]
},
// data2
{
  name : 112, 
  factors : 
  [
    {name:"f1", value:"cat", unit : "g"},
    {name:"f2", value:"13"},
    {name: "f3", value:"rain"}
  ]
}
// more data ...

我想找到一个f3=rainf1=cat

我试过了

query = {
  factors : {
    $elemMatch : 
       [
          {name: "f1", value:"cat"},
          {name: "f3", value: "rain"}
       ]
}, 

但有错误说,     Error: error: { "$err" : "$elemMatch needs an Object", "code" : 12517 }

我该怎么做才能构建该查询?

1 个答案:

答案 0 :(得分:3)

您需要使用$and逻辑查询运算符

pointer-events: none