【问题标题】:MongoDB how to push multiple objects in a collection into an arrayMongoDB如何将集合中的多个对象推送到数组中
【发布时间】:2015-11-20 23:59:24
【问题描述】:

如果我在 mongodb 集合中有多个文档,如下所示:

// document 1
{
    _id: '123',
    date: '5/10/15',

    charges: [{
        amount: 500,
        description: 'foo',
    },{
        amount: 400,
        description: 'bar',
    }],
}


// document 2    
{
    _id: '456',
    date: '5/11/15',

    charges: [{
        amount: 500,
        description: 'foo',
    },{
        amount: 300,
        description: 'foo',
    }],
}

我想创建数量为 500 的所有费用的数组。结果应如下所示:

[{
    amount: 500,
    description: 'foo'
}, {
    amount: 500,
    description: 'foo'
}]

最有效的方法是什么?

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    试试这个:

    db.collection.aggregate(
        [
            { 
                $unwind: "$charges"
            },
            {
                $match: {
                    amount: 500
                }
            }
        ]
    );
    

    【讨论】:

      【解决方案2】:

      在您使用aggregation framework$unwind$group 的文档中:

      db.collection.aggregate([
          // Match documents with the required criteria
          { "$match": { "charges.amount": 500 } },
      
          // Unwind to de-normalize the content
          { "$unwind": "$charges" },
      
          // Filter the de-normalized documents
          { "$match": { "charges.amount": 500 } },
      
          // Group back the result
          { "$group": {
              "_id": null,
              "charges": { "$push": "$charges" }
          }}        
      ])
      

      或者在现代版本中更有效的是先过滤数组:

      db.collection.aggregate([
          // Match documents with the required criteria
          { "$match": { "charges.amount": 500 } },
      
          // Pre filter the array
          { "$redact": {
              "$cond": {
                  "if": { "$eq": [{ "$ifNull": [ "$amount", 500 ] }, 500 ]},
                  "then": "$$DESCEND",
                  "else": "$$PRUNE"
              }
          }},
      
          // Unwind to de-normalize the content
          { "$unwind": "$charges" },
      
          // Group back the result
          { "$group": {
              "_id": null,
              "charges": { "$push": "$charges" }
          }}        
      ])
      

      未来版本(在当前开发版本中工作)将有一个更有用的$filter 方法:

      db.collection.aggregate([
          // Match documents with the required criteria
          { "$match": { "charges.amount": 500 } },
      
          // Filter the array
          { "$project": {
              "charges": {
                  "$filter": {
                      "input": "$charges",
                      "as": "charge",
                      "cond": {
                          "$eq": [ "$$charge.amount", 500 ]
                      }
                  }
              }
          }},
      
          // Unwind to de-normalize the content
          { "$unwind": "$charges" },
      
          // Group back the result
          { "$group": {
              "_id": null,
              "charges": { "$push": "$charges" }
          }}        
      ])
      

      所有结果:

      {
          "_id": null,
          "charges": [
              {
                  amount: 500,
                  description: 'foo'
              }, {
                  amount: 500,
                  description: 'foo'
              }
          ]
      }
      

      【讨论】:

        猜你喜欢
        • 2020-05-21
        • 2016-03-05
        • 2023-01-18
        • 2017-08-03
        • 2017-07-10
        • 1970-01-01
        • 1970-01-01
        • 2021-08-25
        • 1970-01-01
        相关资源
        最近更新 更多