【发布时间】: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