我有一个收藏,其中包含一些文档,如下所示:

{
  "transactionId": 3432423,
  "reviews": [
    {
      "fromUser": {
        "userId": "5236aa1acd6e"
      },
      "toUser": {
        "userId": "0ec8db9544cc"
      },
      "rating": 4.3,
      "comment": ""
    },
    {
    "toUser": {
        "userId": "5236aa1acd6e",
        "email": "[email protected]",
        "firstName": "your",
        "lastName": "life"
      },
      "fromUser": {
        "userId": "0ec8db9544cc",
        "email": "[email protected]",
        "firstName": "my",
        "lastName": "life"
      },
      "rating": 4.3,
      "comment": ""
    }
  ]

}


我需要检查文档中是否存在子文档评论。我用过这个查询,

db.getCollection('_testCollection').find({  "$elemMatch": { "reviews": { "$exists": false } }})


它抛出一个错误,说,

"errmsg" : "unknown top level operator: $elemMatch",

最佳答案

您要使用$elemMatch$exists来确定reviews是否为空数组。

相反,可以与[]进行比较:

db.test.find({reviews: []})


或使用$size运算符:

db.test.find({reviews: {$size: 0}})

关于node.js - Mongodb-未知的顶级运营商:$ elemMatch”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43671965/

10-09 22:20