-2

I have the format of:

{
        "_id" : ObjectId("5af44cfbe2e96c58ec402efb"),
        "username" : "egealpay2",
        "email" : "egealpay2@gmail.com",
        "fullName" : "ege",
        "pnumber" : "",
        "oneSignal" : "d427dcdf-7939-4ada-a035-be74e1e45091",
        "friends" : [
                {
                        "email" : "baransrc@gmail.com",
                        "status" : 2
                }
        ],
        "alarms" : [
                {
                        "id" : "2",
                        "title" : "before",
                        "location" : "default",
                        "startTime" : "2018-05-16\t\t\t16:29",
                        "endTime" : "16:29\t\t\t17 - 05 - 2018",
                        "remindAt" : "17:29\t\t\t15 - 05 - 2018",
                        "privacy" : "0",
                        "userORJoinedAlarm" : "1",
                        "eventJoiners" : [
                                {
                                        "email" : "ilkersadya@sabanciuniv.edu",
                                        "status" : 1
                                },
                                {
                                        "email" : "ilkercankayasss@sabanciuniv.edu",
                                        "status" : 1
                                },
                                {
                                        "email" : "ilkearcankaya@sabanciuniv.edu",
                                        "status" : 1
                                },
                                {
                                        "email" : "iaaalkercankaya@sabanciuniv.edu",
                                        "status" : 1
                                }
                        ],
                        "comments" : [ ]
                }
        ]
}

i would like to set status: 2 of "email" : "ilkersadya@sabanciuniv.edu", its under"alarms:" , "eventJoiners" :which has an id of 2 as"id"`

I have tried using push with $ but couldnt manage to get this working and its been bugging me over 2 hours.

Update i had tried thanks to @Ashish:

 dbo.collection("users").findOne( { email: userId } , function(errZer, addedZer) {
          dbo.collection("users").findOneAndUpdate( {email: addedID, "alarms.id": eventIDGiv,  alarms: { $elemMatch: { id: '2', eventJoiners: { $elemMatch: { email: addedID } } }} },
          { $set: { "alarms.$.eventJoiners.$.status": '2' } , function(err, added) {

got the error

(node:9334) UnhandledPromiseRejectionWarning: MongoError: Too many positional (i.e. '$') elements found in path 'alarms.$.eventJoiners.$.status'
Compu Scie
  • 21
  • 1
  • 5

1 Answers1

3

Positional operator ($) can be used only for one level of nested arrays. In your case you have to specify conditions both for alarms and eventJoiners. To do that you can use $[< identifier >] syntax (which is available in MongoDB 3.6+):

db.collection.update(
    { _id: ObjectId("5af44cfbe2e96c58ec402efb") },
    { $set: { "alarms.$[elem1].eventJoiners.$[elem2].status": "2" } },
    { arrayFilters: [ { "elem1.id": "2" }, { "elem2.email": "ilkersadya@sabanciuniv.edu" } ]})

In this case identifiers elem1 and elem2 are used as placeholders for conditions specifed in arrayFilters section.

mickl
  • 48,568
  • 9
  • 60
  • 89