如何根据 MongoDB 中的条件更新嵌套子文档?
How to update nested subdocument by condition in MongoDB?
我有以下数据结构:
// Journeys collection in MongoDB
{
"_id" : ObjectId("560feabb682290c218770110"),
"title" : "El Camino de Santiago",
"messages" : [
{
"title" : "The sun is strong!",
"text" : "Lorem ipsum and something more text goes here",
"lat" : "-34.5555555",
"lng" : "23.34455335",
"_id" : ObjectId("560feabc682290c218770111")
},
{
"title" : "The hike was great",
"text" : "Lorem ipsum and something more text goes here",
"lat" : "-34.5555555",
"lng" : "23.34455335",
"_id" : ObjectId("560feab9682290c21877010f")
}
],
"followers" : []
}
如何在 此特定旅程 中更新 _id
为 560feab9682290c21877010f
的消息的 title
字段?
给你:
db.journeys.update({'messages.id': ObjectId("560feab9682290c21877010f")}, {$set: {'messages.$.title': 'New Title'}});
我有以下数据结构:
// Journeys collection in MongoDB
{
"_id" : ObjectId("560feabb682290c218770110"),
"title" : "El Camino de Santiago",
"messages" : [
{
"title" : "The sun is strong!",
"text" : "Lorem ipsum and something more text goes here",
"lat" : "-34.5555555",
"lng" : "23.34455335",
"_id" : ObjectId("560feabc682290c218770111")
},
{
"title" : "The hike was great",
"text" : "Lorem ipsum and something more text goes here",
"lat" : "-34.5555555",
"lng" : "23.34455335",
"_id" : ObjectId("560feab9682290c21877010f")
}
],
"followers" : []
}
如何在 此特定旅程 中更新 _id
为 560feab9682290c21877010f
的消息的 title
字段?
给你:
db.journeys.update({'messages.id': ObjectId("560feab9682290c21877010f")}, {$set: {'messages.$.title': 'New Title'}});