FieldPath字段名称不得包含“.".尝试使用AGGREGATE时 [英] FieldPath field names may not contain '.' when trying to use AGGREGATE

查看:545
本文介绍了FieldPath字段名称不得包含“.".尝试使用AGGREGATE时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的查询出了什么问题?

What's wrong with my query?

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 '.'

推荐答案

您可以在下面的查询中使用

You can use below query

如果您使用的是 $expr ,那么您可以轻松地在其中使用聚合运算符.因此,您可以使用 $arrayElemAt 运算符,可为您提供选择所需元素的便利.

If you are using $expr then you can easily use aggregation operator inside it. So Instead of using .dot notation to find the element, you can use $arrayElemAt operator which provides you the facility to choose the element you want.

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 行为 此处

You can check the $expr behaviour here

这篇关于FieldPath字段名称不得包含“.".尝试使用AGGREGATE时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆