问题
I have a mongo collection that looks like this
db.users.find().pretty()
{
"_id" : ObjectId("57c3d5b3d364e624b4470dfb"),
"fullname" : "tim",
"username" : "tim",
"email" : "tim@gmail.com",
"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"
}
]
}
So i eventually would want it to look like this
db.users.find().pretty()
{
"_id" : ObjectId("57c3d5b3d364e624b4470dfb"),`enter code here`
"fullname" : "tim",
"username" : "tim",
"email" : "tim@gmail.com",
"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"
}
]
}
I want to add the stats array to the current exercise array. I am having trouble with the dot notation with a double nested array
i tried this
db.users.update({
'email': 'jeffreyyourman@gmail.com', "workout.workoutId": "Bkb6HIWs" ,"workout.exercises.exerciseId":"ByU8II-s"
},
{
$push: {
"workout.0.exercises.$.stats": {"sets":"sets", "reps":"reps"}}})
Which actually works but will ALWAYS push to the first nested exercises object.
now if i do this...
db.users.update({
'email': 'jeffreyyourman@gmail.com', "workout.workoutId": "Bkb6HIWs" ,"workout.exercises.exerciseId":"ByU8II-s"
},
{
$push: {
"workout.0.exercises.1.stats": {"sets":"sets", "reps":"reps"}}})
and replace the $ with a 1 it will push to the second exercises array which is obviously what i want. But i am building a website so i can't obviously hard code that in. I need to use the $ but it doesn't seem to make it past the first exercises object.
Any help i would greatly appreciate it !
回答1:
There's now (MongoDB >=3.6) a way to do this with the arrayFilters and $[identifier].
The below example is using mongoose and will add an item into an array inside a double nested array. A nice article explaining this is here.
const blogPost = await BlogPost.create({
title : 'A Node.js Perspective on MongoDB 3.6: Array Filters',
comments : [
{ author : 'Foo', text : 'This is awesome!', replies : { name : 'George', seenBy : ['Pacey'] } },
{ author : 'Bar', text : 'Where are the upgrade docs?', replies : { name : 'John', seenBy : ['Jenny'] } }
]
});
const updatedPost = await BlogPost.findOneAndUpdate({ _id : blogPost._id }, {
$addToSet : {
'comments.$[comment].replies.$[reply].seenBy' : 'Jenny'
}
}, {
arrayFilters : [{ 'comment.author' : 'Foo' }, { 'reply.name' : 'George' }],
new : true
});
console.log(updatedPost.comments[0].replies);
回答2:
db.users.update({
'email': 'jeffreyyourman@gmail.com',
"workout.workoutId": "Bkb6HIWs",
"workout.exercises.exerciseId":"ByU8II-s"
},
This query part is returning the first matching object, which is the whole user profile rather than an exercise entry. So you can just specify email to get the same object. "workoutId" and "exerciseId" is redundant.
{
$push: {
"workout.0.exercises.$.stats": {"sets":"sets", "reps":"reps"}
}
})
Thus this push command will only push into the first entry of exercise like Amiram said in comment. In this case, you can retrieve the entire object, modify it, and then save.
But I think you probably need to redesign your schema for better performance. Maybe make a schema for workout and exercise, then use reference to connect them. http://mongoosejs.com/docs/populate.html
来源:https://stackoverflow.com/questions/39199961/mongo-updating-inside-a-double-nested-array