我想根据查询执行后的结果更新一些值。
如果条件为真,我想要 +1 的“comment_sort”值。
条件:如果“comment_group”为1且“comment_sort”大于0
原始数据库是..
{
"_id" : ObjectId("5bc984ef8e798ccb0309ef13"),
"post_no" : 56,
"username" : "a@a.aa",
"nickname" : "nickNameSuccess",
"post_content" : "56",
"post_title" : "56"
"comment" : [
{
"comment_no" : 238,
"comment_sort" : 1, // (+1) "comment_sort" : 2
"comment_depth" : 0,
"comment_group" : 1
},
{
"comment_no" : 240,
"comment_sort" : 2, // (+1) "comment_sort" : 3
"comment_depth" : 1,
"comment_group" : 1
},
{
"comment_no" : 235,
"comment_sort" : 1,
"comment_depth" : 0,
"comment_group" : 2
},
{
"comment_no" : 237,
"comment_sort" : 2,
"comment_depth" : 0,
"comment_group" : 2
}
]
}
查询是..
db.getCollection('post').aggregate([
{"$match" : {"post_no": 56}},
{ "$project": {
"comment": {
"$map": {
"input": "$comment",
"in": {
"comment_no": "$$this.comment_no",
"comment_group": "$$this.comment_group",
"comment_sort": "$$this.comment_sort",
"comment_depth": "$$this.comment_depth"
}
}
}
}},
{ "$unwind": '$comment' },
{"$match" : {"comment.comment_group": 1}},
{"$match" : {"comment.comment_sort": {"$gt":0}} },
// { $group: { _id: null, comment_sort: { $max: "$comment.comment_sort" }}}
])
结果是..
{
"_id" : ObjectId("5bc984ef8e798ccb0309ef13"),
"comment" : {
"comment_no" : 238,
"comment_group" : 1,
"comment_sort" : 1,
"comment_depth" : 0
}
},
{
"_id" : ObjectId("5bc984ef8e798ccb0309ef13"),
"comment" : {
"comment_no" : 240,
"comment_group" : 1,
"comment_sort" : 2,
"comment_depth" : 1
}
}
从这个结果中,我只想增加(+1)“comment_sort”的值。
(例如 1 --> 2、2 --> 3)
如何改进查询?
我想要建议。
问候。