1

Im trying to find the index of an object inside an array and return that index so that I can update my collection with the returned index. Consider this structure in mongoDb:

{

    "_id" : ObjectId("571e32d44990e27409df4bb5”),

    "association" : ”f",

    "events" : [

            {

                    "event" : ”eventtest”,

                    "students" : [

                            {

                                    "session" : "beP--kELak7gcFQLN6-o_AdTkJA8eYcp",

                                    "name" : ”Student",

                                    "email" : ”email@example.com”,

                                    "checkedIn" : false

                            },

                            {

                                    "name" : "newstudent",

                                    "email" : "newemail",

                                    "checkedIn" : false

                            }

                    ]

            }

}

When I enter the students email in a form I want to set its "checkedIn" value to true. Using mongodb, express and node js this is what I've managed to do so far:

MongoClient.connect(url, function(err, db){
if (err){
console.log("Unable to connect to server");
} else {
  console.log('Connected to server');
  var collection = db.collection('associations');
  //indexes should be set dynamically where the emails match
  var eventIndex = 0;
  var studentIndex = 1;
  var setModifier = { $set: {} };
  setModifier.$set['events.' + eventIndex + '.students.' + studentIndex + '.checkedIn'] = true;
  collection.update({
    association: assoc,
    "events.event": event,
    "events.students.email": req.body.email
  }, setModifier, function(err, user){
    if(err) {
      console.log(err);
    } else{
      res.redirect("checkin");
    }
    db.close();
  });

}

});

This works just fine, I need to query the student with, for example, the email of "newemail" and set the studentIndex to 1, and the same to return a value of 0 for the event: "eventtest".

I've found that you can use mapReduce or aggregation to find an items index in an array but I'm not sure that this would work for objects and I have no idea where to start. Someone suggested processing this on the client side as mapReduce wasn't recommended in the first place, but this seems like quite a lot of hassle.

Here are some links that might shed to some light over what I'm trying to accomplish.

Retrieve only the queried element in an object array in MongoDB collection

Retrieve index of item in array in MongoDB

Using MongoDB's positional operator $ in a deeply nested document query

function to return index of an object in a deeply nested array

EDIT: I found this article aswell, Mongodb sort inner array

Maybe it could work if I aggregate the inner array of students and sort the student objects so that the corresponding student that matches the email is sorted at the top of the list while still keeping the rest of the structure in the whole document the same. This way the student that should be checked in always has an index of 0? Is this bad practice? It seems to me that this is a a lot of hassle for a rather simple task.

Community
  • 1
  • 1
Anton Scotte
  • 439
  • 5
  • 17

0 Answers0