【问题标题】:MongoDB aggregate: retrieve data in single arrayMongoDB聚合:检索单个数组中的数据
【发布时间】:2023-04-10 02:33:01
【问题描述】:

我有数百万个具有以下架构的文档:

{
   _id: '3fbwehgzgfwehgrqwegrqwer',
   someData: [0,1],
   moreData: {
       key: true
   } 
},

{
   _id: '24nj5h219ebwjfqwverqwer',
   someData: [2,3],
   moreData: {
       key: true
   } 
},

我需要将 someData 数组组合在一个结果数组中,例如:

{
    result: [
       [0,1],
       [2,3] 
    ]
}

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    使用aggregation pipeline,您可以通过_id: null$group,而不是$push$someDataresult字段中的所有$someData

    db.collection.aggregate([
        {"$group":{_id: null, result: {$push: "$someData"}}} 
    ]).pretty()
    

    结果:

    { "_id" : null, "result" : [ [ 0, 1 ], [ 2, 3 ] ] }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 2017-02-18
      相关资源
      最近更新 更多