【问题标题】:Mongo Aggregation Project add property to all in ArrayMongo 聚合项目将属性添加到数组中的所有内容
【发布时间】:2017-12-07 11:15:21
【问题描述】:

我正在使用 mongo 2.6,作为聚合管道的一部分,我需要向数组中的所有条目添加一个对象。由于我使用 mongo 2.6,我无法使用 $addFields。

我的数据如下:

{
"_id" : ObjectId("5a290637e4b08ddd7334e541"),
"placeName" : "Caspeco Mobile Order",
"created" : ISODate("2017-12-07T09:13:27.248Z"),
"currency" : "SEK",
"isCredit" : false,
"costCenter" : {
    "name" : "Test Restaurangen",
    "number" : "800"
},
"debitAccounts" : [ 
    {
        "name" : "CASH",
        "number" : "1915",
        "amount" : 189.0
    }
],
"creditAccounts" : [ 
    {
        "name" : "Mat",
        "number" : "3120",
        "amount" : 168.749984741211
    }
],
"taxAccounts" : [ 
    {
        "name" : "Moms 12%",
        "number" : "2621",
        "amount" : 20.2500076293945
    }
],
"credit" : false

}

作为聚合的第一步,我需要将 costCenter 对象添加到所有潜在的 creditAccounts

做类似的事情:

{$project: {'creditAccounts'{'costCentre':'$costCenter.name','costCentreAccount':'$costCenter.number','name':'$creditAccounts.name','number':'$creditAccounts.number','amount':'$creditAccounts.amount'}, document: '$$ROOT'}}

没有用,因为它会生成一组名称和数字...

【问题讨论】:

    标签: mongodb aggregation-framework projection


    【解决方案1】:

    找到解决方案是$map:

    creditAccounts:{$map:{input:'$creditAccounts', as:'creditAcccount', in : {name:'$$creditAcccount.name',number:'$$creditAcccount.name',amount:'$$creditAcccount.amount', costCentre:'$costCenter.name',costCentreAccount:'$costCenter.number'}}}
    

    虽然它真的很丑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 2021-05-27
      • 1970-01-01
      相关资源
      最近更新 更多