我有一个集合users,其中每个都包含属性,gyms例如:
{
    "_id" : ObjectId("aaaaa"),
    "firstName" : "first",
    "lastName" : "second",
    "email" : "aaa@aaaa",
    "password" : "aaaaa",
    "gyms" : [
        {
            "name" : "aaaaaa",
            "address" : "aaaaaaaaaaaaa"
        }
    ]
}
但是,当我运行时,db.users.aggregate(..)我得到:
exception: Value at end of $unwind field path '$gyms' must be an Array, but is a Object
似乎一些用户文档包含健身房:{} 或根本没有健身房而不是数组。我需要找到这些文件,我该怎么做?
编辑:
我运行的聚合命令:
db.users.aggregate({ $unwind: "$gyms" }, { $match: { "gyms.address": { $exists: true } } } )
试试这个也许对你也有帮助
db.users.aggregate([{
  "$match": {
      "$nor": [{
          "gyms": {
              "$exists": false
          }
      }, {
          "gyms": {
              "$size": 0
          }
      }, {
          "gyms": {
              "$size": 1
          }
      }]
  }
 }, {
  "$unwind": "$gyms"
}]).pretty()