FieldPath 字段名称不能包含“.”当尝试使用 AGGREGATE 时

2024-05-09

我的查询有什么问题吗?

db.table.aggregate([
  {
    '$match' => {
      '$expr' => {
        '$and' => [
          { '$eq' => [{ '$size' => '$events' }, 4] },
          { 'events.0.updated' => { '$lt' => '2019-05-05' } }
        ]
      }
    }
  }
])

我越来越:

Mongo::Error::OperationFailure: FieldPath field names may not contain '.'

您可以使用以下查询

如果您正在使用$expr https://docs.mongodb.com/manual/reference/operator/query/expr/那么您可以轻松地在其中使用聚合运算符。所以而不是使用.dot符号来查找元素,您可以使用$arrayElemAt https://docs.mongodb.com/manual/reference/operator/aggregation/arrayElemAt/运算符使您可以方便地选择所需的元素。

db.table.aggregate([
  { '$match': {
    '$expr': {
      '$and': [
        { '$eq': [{ '$size': '$events' }, 4] },
        { '$lt': [{ '$arrayElemAt': ['$events.updated', 0] }, '2019-05-05'] }
      ]
    }
  }}
])

或者你试图做的方式

db.collection.aggregate([
  { "$match": {
    "events.0.updated": { "$lt": "2019-05-05" },
    "$expr": { "$eq": [{ "$size": "$events" }, 4] }
  }}
])

您可以检查$expr https://docs.mongodb.com/manual/reference/operator/query/expr/行为here https://docs.mongodb.com/manual/meta/aggregation-quick-reference/#aggregation-expressions

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

FieldPath 字段名称不能包含“.”当尝试使用 AGGREGATE 时 的相关文章

随机推荐