クエリ実行後の結果に応じて値の一部を更新したい。
条件が真の場合、「comment_sort」値を+1したいもの。
条件 : 「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
}
}
この結果から、「comment_sort」の値だけを増加(+1)したいと思います。
(例 1 --> 2、2 --> 3)
クエリを改善するにはどうすればよいですか?
アドバイスをお願いします。
よろしく。