MongoDB,为什么我的查询中需要$ elemMatch?

时间:2019-04-20 17:44:36

标签: mongodb querying

我有这样的文件:

curl -X POST -H "Authorization: key=<your key from the firebase console> -H "Content-Type: application/json" -d '{
  "notification": {
    "title": "Yo friend says",
    "body": "Pay attention to me",
  },
  "to": <unique device token>
}' "https://fcm.googleapis.com/fcm/send"

我有此查询(版本1):

{
  "_id" : ObjectId("5cba0179577fa92b9b6b36e8"),
  "demographics" : [
    {
      "key" : "country_code",
      "value" : "DE"
    },
    {
      "key" : "gender",
      "value" : "male"
    }
  ]
}

我还有另一个(版本2):

$match: {
  "demographics": {
    $elemMatch: {
      "key": "gender",
      "value": "female"
    }
  }
}

它们看起来都返回相同的结果,我没有经过仔细测试,但是我发现版本2不太冗长,更直观。

0 个答案:

没有答案