新手帮助! 我想要帮助更新Object中的一个值,该对象位于Object中包含的数组中!
{ "_id" : ObjectId("5a641fc1b66d0a1b7c274c99"),
"comments" : [ { "author" : { "id" : ObjectId("5a5c93954c5b6f35953d38e0"),
"username" : "Anton" },
"_id" : ObjectId("5a641fceb66d0a1b7c274c9a"),
"text" : "I love this place!", "__v" : 0 },
{ "author" : { "id" : ObjectId("5a5c93954c5b6f35953d38e0"),
"username" : "Anton" },
"_id" : ObjectId("5a642023b66d0a1b7c274c9b"),
"text" : "Supercool!", "__v" : 0 } ],
"campGroundName" : "Desert Creek",
"campGroundImage":
"https://farm8.staticflickr.com/7205/7121863467_eb0aa64193.jpg",
"description" : " Donec rhoncus, enim vel semper pharetra, dui justo accumsan neque, nec dictum tortor nibh id es", "__v" : 2,
"addedBy" : { "id" : ObjectId("5a5c93954c5b6f35953d38e0"), "username" : "Anton" } }`
这里我想访问对象的comments数组中的text值。
如何更新Mongoose中的文本值?
提前致谢!
答案 0 :(得分:0)
使用$ elemMatch查找查询以在嵌套数组中搜索,然后使用“myarrayfield。$。keyoftheobjectinarray”进行更新:
示例在与u == 1
匹配的嵌套对象数组中用10更新所有u键db.a.update({"a": {$elemMatch:{"u": 1}}},{$set:{"a.$.u": 10}},{multi: true, upsert:false})