1

I am currently working on an application that takes control of Projects, which have Meetings and that these meetings have Participants.

I want to consult a Participant by his nomina field.

Structure for a project document object:

 {
       "id":"5c1b0616a0441f27f022bfdc",
       "name":"Project Test",
       "area":"Area",
       "date":"2019-01-01",
       "meetings":[
          {
             "id":"5c1b073d445707834699ce97",
             "objetive":"Objetive",
             "fecha":"2019-01-01",
             "participants":[
                {
                   "nomina":1,
                   "name":"Person 1",
                   "role":"Rol1",
                   "area":"area1",
                   "signature":null
                },
                {
                   "nomina":2,
                   "name":"Person 2",
                   "role":"rol 2",
                   "area":"área 2",
                   "signature":null
                }
             ]
          }
       ]
    }

Expected behavior

I want to consult a Participant by nomina field knowing the id of the Project and also knowing the id of the Meeting.

Expected output

Having:

  • id Project = 5c1b0616a0441f27f022bfdc
  • id Meeting = 5c1b073d445707834699ce97
  • nomina Participant = 1

It's expected that the query will return me:

{    
"nomina":1,             
"name":"Person 1",
"role":"Rol1",
"area":"area1",
"signature":null
}

1 Answer 1

1

For not so huge number of meetings in every document if you want to get the exact document stated, you can do this pipeline, it is straight forward:

db.collection.aggregate(
    [
       {
            $match: {
              id:"5c1b0616a0441f27f022bfdc"             
            }
        },          {
            $unwind: {
                path : "$meetings"
            }
        },
        {
            $unwind: {
                path : "$meetings.participants"
            }
        },
        {
            $match: {
              "meetings.id":"5c1b073d445707834699ce97",
              "meetings.participants.nomina":1

            }
        },
        {
            $replaceRoot: {
                newRoot: "$meetings.participants"
            }
        }
    ]);

If you would have over thousands of elements in meetings then I'd suggest adding another match to meetings or grouping meetings and project IDs. But if you just want to get the document containing what you want it is just a simple find query:

db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 });
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, thanks. db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 }); this will always return me the entire document? I ran the example here
Yes, that would bring the whole document containing the conditions you want, but if you have at most a couple of thousand elements in "meetings" array, you can easily go with aggregation pipeline.
Sorry, and now that I know the Participant with nomina= 1, how could modify his signature field?
You mean you want to update that document ? nodechef.com/docs/cloud-search/… Something like this should work: db.collection.update({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 },{$set:{"meetings.participants.$$.signature":1122}}, false, true )
Oh yup, sorry about that, what is the tool or language that your are working with mongo on ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.