下面给出的是来自 my_collection
的示例文档
{
"_id":ObjectId("5b3f4e1013aad111501b34c4")
"Text":["kjsdf sdfjgnks vk ekl sdsdd."],
"Title":["lkdjf wufk"],
"List":["3412","1244","7654","8476"],
"__v":0
}
我想聚合
my_collection
使得:所以我写了这段代码:
db.my_collection.aggregate(
[
{
$addFields: {
totalList: { $size: "$List" } // I can achieve the 1st point with this
}
},
{ $project : {
__v:0, // I can achieve the 2nd point with this
List:
{
$reduce: // I can achieve the 3rd point with this
{
input: "$List",
initialValue: "",
in: { $concat : ["$$value", "$$this", ","] }
}
}
}
}
]
);
但是,当我运行上述命令时出现以下错误:
Failed to execute a script.
Error:
Assert: command failed: {
"ok" : 0,
"errmsg" : "Bad projection specification, cannot include fields or add computed fields during an exclusion projection: { __v: 0.0, List: { $reduce: { input: \"$List\", initialValue: \"\", in: { $concat: [ \"$$value\", \"$$this\", \",\" ] } } } }",
"code" : 40182,
"codeName" : "Location40182"
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
DBCollection.prototype.aggregate@:1:355
@(shell):1:1
Error: command failed: {
"ok" : 0,
"errmsg" : "Bad projection specification, cannot include fields or add computed fields during an exclusion projection: { __v: 0.0, List: { $reduce: { input: \"$List\", initialValue: \"\", in: { $concat: [ \"$$value\", \"$$this\", \",\" ] } } } }",
"code" : 40182,
"codeName" : "Location40182"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
DBCollection.prototype.aggregate@:1:355
@(shell):1:1
因此,我将其改写为:
db.my_collection.aggregate(
[
{
$addFields: {
totalList: { $size: "$List" } // Achieve the 1st point with this
}
},
{ $project : {
"Text":1,
"Title":1,
__v:{ // Achieve the 2nd point with this
$cond: {
if: { $eq: [1,1] },
then: "$$REMOVE",
else: 0
}
},
List:
{
$reduce: // Achieve the 3rd point with this
{
input: "$List",
initialValue: "",
in: { $concat : ["$$value", "$$this", ","] }
}
}
}
}
]
);
现在,我可以达到前 3 分。
但是,我怎样才能达到第 4 点和第 5 点?
最佳答案
您可以尝试以下聚合
db.collection.aggregate([
{ "$addFields": {
"totalList": { "$size": "$List" },
"List": {
"$substr": [
{ "$reduce": {
"input": "$List",
"initialValue": "",
"in": { "$concat": ["$$value", ",", "$$this"] }
}},
1,
50
]
}
}},
{ "$project": { "List": 1, "Test": 1, "Title": 1, "totalList": 1 }}
])
输出
[
{
"List": "3412,1244,7654,8476",
"Title": [
"lkdjf wufk"
],
"_id": ObjectId("5b3f4e1013aad111501b34c4"),
"totalList": 4
}
]
试试看 here
关于MongoDB:如何在聚合上应用多个 $project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51319958/