【问题标题】:How could I get the matched nested items in array如何在数组中获取匹配的嵌套项
【发布时间】:2015-06-23 03:47:22
【问题描述】:

如何在数组中获取匹配的嵌套项

我想返回嵌套数组中的匹配项。

例如,我想过滤掉 items

中包含 "A428 ","A429 " 的记录

我怎么能得到它?

查询

pipeline_work = [
  { '$match': 'records.items': '$in': ["A428 ","A429 "])}
]
  cur = db[source_collection].runCommand('aggregate',pipeline: pipeline_work , allowDiskUse: true)

示例文档

  {
    "_id": "0007db2dac8d6482ec60c228b700c3ec",
    "records": [
      {
        "APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
        "FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
        "items": [
          "A428 ",
          "     ",
          "     "
        ]
      },
      {
        "APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
        "FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
        "items": [
          "A429 ",
          "     ",
          "     "
        ]
      },
      {
        "APPL_DATE": new Date("1996-04-15T08:00:00+0800"),
        "FUNC_DATE": new Date("1996-03-18T08:00:00+0800"),
        "items": [
          "A180 ",
          "     ",
          "     "
        ]
      }]

预期结果

  "_id": "0007db2dac8d6482ec60c228b700c3ec",
  "records": [
    {
      "APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
      "FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
      "items": [
        "A428 ",
        "     ",
        "     "
      ]
    },
    {
      "APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
      "FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
      "items": [
        "A429 ",
        "     ",
        "     "
      ]
    }]

因 {"errmsg":"exception: 管道阶段规范对象必须只包含一个字段而失败。","code":16435,"ok":0}

pipeline_work = [
  {
    "$unwind": "$records",
    "$match": {
      "records.items": {
        "$in": ["A428 ", "A429 "]
      }
    },
    "$group": {
      "_id": "$_id",
      "records": {
        "$push": "$records"
      }
    }
  }, {
    '$limit': 1
  }
];

【问题讨论】:

  • $unwind records 先数组再match

标签: mongodb


【解决方案1】:

你应该首先$unwindrecords然后match元素如下:

db.collection.aggregate({
    "$unwind": "$records"
}, {
    "$match": {
    "records.items": {
        "$in": ["A428 ", "A429 "]
    }
    }
}, {
    "$group": {
    "_id": "$_id",
    "records": {
        "$push": "$records"
    }
    }
}).pretty()

【讨论】:

  • 您好,您的解决方案将因`管道阶段规范对象必须包含一个字段而失败。`
  • @newBike 不,它会给你预期的输出。你试过了吗?仅当您尝试将任何内容推送到 _id 之外时才会发生此异常。
【解决方案2】:

请检查以下查询:

 db.collection.aggregate([
   {"$unwind" : "$records"},
   {"$unwind" : "$records.items"},
   {"$match" : {"records.items" : {"$in" : [ "A428 ","A429 "] } } },
   {"$group" : { "_id" :  { "id" : "$_id" , "app_date" : "$records.APPL_DATE" , 
    "fun_date" : "$records.FUNC_DATE"} , 
    "records_temp" : { "APPL_DATE" : { "$first" :"$records.APPL_DATE" },
    "FUNC_DATE" : { "$first" : "$records.FUNC_DATE" } ,
    "items" : {"$push" : "$records.items"} } },
  {"$group" : { "_id" : "$_id.id", "records" : { "$push" : "$records_temp" } } }
 ]);

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2021-03-18
    • 2013-12-31
    • 1970-01-01
    相关资源
    最近更新 更多