1

Below is the code I am trying but unable to get the desired results. matching on jobid and email.

    mongoConn.aggregate({
                    collectionName: Collection.jobactivity,
                    data: [
                        { $unwind: '$jobs' },
                        { $match: { '$and': [{ 'jobs.jobid': candidateJobHistoryObj.jobid}, {'jobs.activity.email':  candidateJobHistoryObj.candidateemail  }] }},
                        { $project: ({ '_id': 0, 'jobs.activity.activityname': 1, 'jobs.activity.firstname': 1, 'jobs.activity.middlename': 1, 'jobs.activity.lastname': 1, 'jobs.activity.createddatetime': 1, 'jobs.activity.createdbyuserid': 1, 'jobs.activity.modifiedtime': 1, 'jobs.activity.modifiedbyuserid': 1 }) },
                        { $sort: { 'jobs.activity.createddatetime': -1 } }
                    ],
                })

Json code present in mongodb:

    {
        "_id" : ObjectId("61c9d42fc87b10aab2e6732a"),
        "accountid" : 122,
        "jobs" : [
            {
                "jobid" : 12,
                "activity" : [
                    {
                        "firstname" : "Vikas Yadav Bhai",
                        "middlename" : null,
                        "lastname" : "Yadav",
                        "email" : "candidate1@abc.com",
                        "activityname" : "inserted the data into candidate engagement workflow",
                        "modifiedbyuserid" : 151,
                        "modifiedtime" : ISODate("2015-12-01T05:30:00.000+05:30"),
                        "createdbyuserid" : 151,
                        "createddatetime" : ISODate("2015-08-12T00:00:00.000+05:30")
                    },
                    {
                        "firstname" : "Vikas Yadav Bhai",
                        "middlename" : null,
                        "lastname" : "Yadav",
                        "email" : "candidate2@abc.com",
                        "activityname" : "updated the job details",
                        "modifiedbyuserid" : 151,
                        "modifiedtime" : ISODate("2015-12-01T05:30:00.000+05:30"),
                        "createdbyuserid" : 151,
                        "createddatetime" : ISODate("2015-08-12T00:00:00.000+05:30")
                    }
    ]
    }
    ]
    }

I want to match the documents where email is => candidate1@abc.com and jobid = 12

ray
  • 11,310
  • 7
  • 18
  • 42
Biku7
  • 450
  • 6
  • 16
  • 1
    Does this answer your question? [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – computercarguy Dec 27 '21 at 16:43
  • Looks like you have a typo problem on your $project stage. Remove the parenthesis. – Bloodbee Dec 27 '21 at 17:28
  • @computercarguy I am not sure if OP wants to process the json object on JS level. Usually matching in MongoDB query would be a more performant way. – ray Dec 30 '21 at 08:49

0 Answers0