【问题标题】:mongodb $unwind with three nested arrays带有三个嵌套数组的 mongodb $unwind
【发布时间】:2019-05-20 15:45:36
【问题描述】:

我的数据有几个嵌套级别:root --> blocks --> children --> “字符串数组”

我想使用带有 $unwind 的聚合来从 “块”级别和最低级别“字符串数组”。

我的问题:根据下面的数据图,使用aggregate和$unwind输出这些字段的代码是什么?

Fields from BLOCKS
   block_id
   block_type  
   definition   
All entries from the lowest level array of strings 

这与我发现的其他帖子不同,因为我想要一些级别而跳过其他级别,还因为较低的两个级别没有名称。

输出中的重复行不是首选,但我可以在稍后阶段进行重复数据删除。

我上传了一张数据结构的图片。如果你能看到图片,我想要的字段周围会有一个蓝色框。

Using "MongoDB Compass", I can see this structure  
ROOT ARRAY
 _id
 edited_on
    -->BLOCKS ARRAY (array of type object)
         block_id
         block_type
         definition
         edit_info (object)
            edited_on
         fields (object)
            display_name
            -->CHILDREN ARRAY (array of type array)
               -->ARRAY (array of type string)
                  0: string
                  1: string

前两个数组的名称分别为“blocks”和“children”。最后两个数组没有名字。

这可能会有所帮助。我已经有一个 $unwind 查询,它为我提供了一些来自根和块级别的字段。

mongo.exe MyDatabase --quiet --eval "printjson(db.modulestore.structures.aggregate( { $unwind: '$blocks' }, { $project: { _id: 1 ,edited_on: 1 , 'definition' : '$blocks.definition' , 'block_type': '$blocks.block_type' , 'block_id': '$blocks.block_id' , 'block_edited_on': '$blocks.edit_info.edited_on' , 'display_name': '$blocks .fields.display_name' } } , { $match: { block_type: 'openassessment' } } ).toArray())" > C:\Data\MyOutput.json

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    没有达到你的预期,我猜这可能对你有帮助

         `db.modulestore.structures.aggregate([
      {
        "$unwind": "$blocks"
      },
      {
        "$unwind": "$fields.children"
      },
      {
        "$project": {
          "_id": 1,
          "edited_on": 1,
          "definition": "$blocks.definition",
          "block_type": "$blocks.block_type",
          "block_id": "$blocks.block_id",
          "block_edited_on": "$blocks.edit_info.edited_on",
          "display_name": "$blocks.fields.display_name"
        }
      },
      {
        "$match": {
          "block_type": "openassessment"
        }
      }
    ]` )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 2019-12-11
      • 1970-01-01
      相关资源
      最近更新 更多