2

How can I find and update the field inside the data arrays

    "_id" : ObjectId("5bfe6df9777d13255bb2ad7c"),
    "program_id" : ObjectId("5b7bcc8520a2b13177b4d92e"),
    "week_id" : "2018.10.48",
    "month" : 10.0,
    "week_count" : 48.0,
    "year" : 2018.0,
    "data" : [ 
        {
            "_id" : ObjectId("5bfe6df9c1e35824dfcce39f"),
            "date" : 27.0,
            "user_ids" : [ 
                {
                    "checkin_time" : 1543400937337.0,
                    "by" : ObjectId("5bb2aa12460b5f2ef97388e6"),
                    "checkin_by" : ObjectId("5bf7dd8387550226071eda5c"),
                    "checkin_status" : 1.0,
                    "point" : 1.0
                }, 
                {
                    "checkin_time" : 1543400937337.0,
                    "by" : ObjectId("5bb453b44817792ec972c1ab"),
                    "checkin_by" : ObjectId("5be7dd8387550226071eda5c"),
                    "user_utc_checkin_time" : 1543401130940.0,
                    "user_time_zone" : "Asia/Kolkata",
                    "checkin_status" : 1.0,
                    "point" : 1.0
                }, 
                {
                    "checkin_time" : 1543400937337.0,
                    "by" : ObjectId("5bed113bcd1c075e045aa79e"),
                    "checkin_by" : ObjectId("5bed113bcd1c075e045aa79e"),
                    "user_utc_checkin_time" : 1543401594869.0,
                    "user_time_zone" : "Asia/Kolkata",
                    "checkin_status" : 1.0,
                    "point" : 1.0
                }
            ]
        }
    ]

I am using

db.collectionName.findAndUpdate(
     {"data.user_ids.checkin_by":mongoose.Types.ObjectId(a) },  
     { $set :
        { "data.user_ids.$.checkin_status":2 }
     }
   )
0

1 Answer 1

0

Try using this:

db.collectionName.update(
{"data.user_ids.checkin_by":mongoose.Types.ObjectId(a) },
{"$set":{"data.user_ids.$.checkin_status":2 }}
)
Sign up to request clarification or add additional context in comments.

6 Comments

no its not working
What happens when you use this code?
"msg": "internal Server Error", "err": { "name": "MongoError", "message": "cannot use the part (data of data.user_ids.0.checkin_status) to traverse the element ({data: [ { date: \"\", user_ids: ...
this is working for me CheckInDao.findAndUpdate({ $and:[ {"_id":mongoose.Types.ObjectId("5c3635b00f62312e2412e124")}, { "data.0.user_ids.checkin_by": mongoose.Types.ObjectId("5c002ac43716336b67bd7726")} ]}, // }, { $set : { "data.0.user_ids.$.checkin_status":11 }} ); but I am still not convince as i have to use data.o.user_ids
try using $ after data instead of 0
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.