2015-10-03 3 views
1

У меня есть следующие структуры данных:Как обновить вложенный поддокумент по условию в 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" : [] 
} 

Как сделать обновление для title поля сообщения с _id из 560feab9682290c21877010f в это конкретное путешествие?

ответ

1

Здесь вы идете:

db.journeys.update({'messages.id': ObjectId("560feab9682290c21877010f")}, {$set: {'messages.$.title': 'New Title'}}); 
Смежные вопросы