我已将Mongo升级到3.2,并且很高兴使用$ slice聚合工作。但是,我的问题是我想将值分配给对象而不是数组。我找不到怎么做。
我的脚本已应用:
db.std_sourceBusinessData.aggregate(
{ $match : {objectType: "Account Balances"}},
{ $project: {_id: 1,entity_id: 1,accountBalances: 1}},
{ $unwind: "$accountBalances" },
{ $match: {"accountBalances": "Sales"}},
{$project: {
_id :1,
"Value" : {$slice: ["$accountBalances",1,1]},
"key" : {$literal: "sales"},
"company": "$entity_id"
}}
)
回来:
{
"_id" : ObjectId("566f3da3d58419e8b0fc76c7"),
"Value" : [
"5428.64"
],
"key" : "sales"
}
请注意,Value是一个数组。我想要的是:
{
"_id" : ObjectId("566f3da3d58419e8b0fc76c7"),
"Value" : "5428.64",
"key" : "sales"
}
谢谢,马特