node.js - Mongo updating inside a double nested array -
i have mongo collection looks 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 want 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 add stats array current exercise array. having trouble dot notation 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 works push first nested exercises object.
now if 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 $ 1 push second exercises array want. building website can't hard code in. need use $ doesn't seem make past first exercises object.
any appreciate !
db.users.update({ 'email': 'jeffreyyourman@gmail.com', "workout.workoutid": "bkb6hiws", "workout.exercises.exerciseid":"byu8ii-s" },
this query part returning first matching object, whole user profile rather exercise entry. can specify email same object. "workoutid" , "exerciseid" redundant.
{ $push: { "workout.0.exercises.$.stats": {"sets":"sets", "reps":"reps"} } })
thus push command push first entry of exercise amiram said in comment. in case, can retrieve entire object, modify it, , save.
but think need redesign schema better performance. maybe make schema workout , exercise, use reference connect them. http://mongoosejs.com/docs/populate.html
Comments
Post a Comment