0
我有一個蒙戈集合,看起來像這樣雙重嵌套數組
db.users.find().pretty()
{
"_id" : ObjectId("57c3d5b3d364e624b4470dfb"),
"fullname" : "tim",
"username" : "tim",
"email" : "[email protected]",
"password" : "$2a$10$.Z9CnK4oKrC/CujDKxT6YutohQkHbAANUoAHTXQp.73KfYWrm5dY2",
"workout" : [
{
"workoutId" : "Bkb6HIWs",
"workoutname" : "chest day",
"BodyTarget" : "Chest",
"date" : "Monday, August 29th, 2016, 2:27:04 AM",
"exercises" : [
{
"exerciseId" : "Bym88LZi",
"exercise" : "bench press",
"date" : "Monday, August 29th, 2016, 2:29:30 AM"
},
{
"exerciseId" : "ByU8II-s",
"exercise" : "flys",
"date" : "Monday, August 29th, 2016, 2:29:34 AM"
}
]
},
{
"workoutId" : "Bk_TrI-o",
"workoutname" : "Back day",
"BodyTarget" : "Back",
"date" : "Monday, August 29th, 2016, 2:27:12 AM"
}
]
}
內蒙戈更新所以我最終還是希望它看起來像這樣
db.users.find().pretty()
{
"_id" : ObjectId("57c3d5b3d364e624b4470dfb"),`enter code here`
"fullname" : "tim",
"username" : "tim",
"email" : "[email protected]",
"password" : "$2a$10$.Z9CnK4oKrC/CujDKxT6YutohQkHbAANUoAHTXQp.73KfYWrm5dY2",
"workout" : [
{
"workoutId" : "Bkb6HIWs",
"workoutname" : "chest day",
"BodyTarget" : "Chest",
"date" : "Monday, August 29th, 2016, 2:27:04 AM",
"exercises" : [
{
"exerciseId" : "Bym88LZi",
"exercise" : "bench press",
"date" : "Monday, August 29th, 2016, 2:29:30 AM",
"stats" : [
{
"reps: '5',
"weight":'105'
}
},
{
"exerciseId" : "ByU8II-s",
"exercise" : "flys",
"date" : "Monday, August 29th, 2016, 2:29:34 AM"
}
]
},
{
"workoutId" : "Bk_TrI-o",
"workoutname" : "Back day",
"BodyTarget" : "Back",
"date" : "Monday, August 29th, 2016, 2:27:12 AM"
}
]
}
我想補充的統計數據陣列到當前的練習數組。我有一個雙重嵌套數組
點符號麻煩我想這
db.users.update({
'email': '[email protected]', "workout.workoutId": "Bkb6HIWs" ,"workout.exercises.exerciseId":"ByU8II-s"
},
{
$push: {
"workout.0.exercises.$.stats": {"sets":"sets", "reps":"reps"}}})
哪些實際工作,卻總是推到第一嵌套的練習對象。
現在如果我這樣做...
db.users.update({
'email': '[email protected]', "workout.workoutId": "Bkb6HIWs" ,"workout.exercises.exerciseId":"ByU8II-s"
},
{
$push: {
"workout.0.exercises.1.stats": {"sets":"sets", "reps":"reps"}}})
和替換用1 $它會推到第二演習陣列這顯然是我想要的。但我正在建立一個網站,所以我不能明確地硬編碼。我需要使用$,但它似乎並沒有讓它通過第一個練習對象。
任何幫助,我將不勝感激!
'$'表示第一個。 mongo應該知道你想更新哪個索引?查詢只是說出要找的東西。我認爲你需要找到文檔,在服務器中更新並保存。 –
如果鍛鍊次數增加沒有任何界限,您可能需要重新考慮您的模式設計,因爲文檔在某個時間點可能會超過16MB的大小限制。 –