Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //my data
- {
- "_id" : ObjectId("57c3d5b3d364e624b4470dfb"),
- "workout" : [
- {
- "workOutId" : "WORK222",
- "exercise" : [
- {
- "exerciseId" : "EX21",
- "state" : [
- {
- "rep" : "115",
- "weight" : "400"
- }
- ]
- },
- {
- "exerciseId" : "EX22",
- "state" : [
- {
- "rep" : "120",
- "weight" : "250"
- }
- ]
- }
- ]
- },
- {
- "workOutId" : "WORK123",
- "exercise" : [
- {
- "exerciseId" : "EX41",
- "state" : [
- {
- "rep" : "150",
- "weight" : "200"
- }
- ]
- },
- {
- "exerciseId" : "EX55",
- "state" : [
- {
- "rep" : "110",
- "weight" : "250"
- }
- ]
- }
- ]
- }
- ]
- }
- //change the state[] array element filed where "rep" = 115 and change the "weight" = 600?
- //with elemMatch
- /* db.getCollection('random').update(
- {
- "workout.exercise.state":{
- $elemMatch: {
- "rep": "115"
- }
- } //query the exact array element
- }, //querry,
- {
- $set: {
- "workout.$[i].exercise.$[j].state.$.weight": "600" //update the value
- }
- }, //update,
- {
- arrayFilters: [
- {
- "i.workOutId": "WORK222" // querry the workout[] array with the element
- },
- {
- "j.exerciseId": "EX21" //querry the exercise[] array with the element
- }
- ]
- } //option
- )*/
- //Without elemMatch with $[<identyfier>]
- /*
- db.getCollection('random').update(
- {//empty} //querry
- {
- $set: {
- "workout.$[i].exercise.$[j].state.$[k].weight": "500"
- }
- }, //update,
- {
- arrayFilters: [
- {
- "i.workOutId": "WORK222" //querry the workout[] array
- },
- {
- "j.exerciseId": "EX21" //querry the exercise[] array
- },
- {
- "k.rep": "115" //queery the state[] array
- }
- ]
- } //option
- )
- */
- /*update all array filed base on eleMatch*/
- /*db.getCollection('random').update(
- {
- "workout.exercise.state": {
- $elemMatch : {
- rep: "110"
- }
- },
- },
- {
- $set: {
- "workout.$[].exercise.$[].state.$.weight": "250"
- }
- }
- )*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement