-1

I have a Mongodb Data which looks like this

{
    "userId" : "123",
    "dataArray" : [
        {
            "scheduledStartDate" : ISODate("2018-08-30T11:34:36.000+05:30"),
            "scheduledEndDate" : ISODate("2018-08-30T11:34:36.000+05:30"),
            "Progress" : 0,
            "ASD":""
        },
        {

            "scheduledStartDate" : ISODate("2018-09-22T11:34:36.000+05:30"),
            "scheduledEndDate" : ISODate("2018-10-01T11:34:36.000+05:30"),
            "Progress" : 0,
            "ASD":ISODate("2018-08-30T11:34:36.000+05:30"),
        }
    ],
    "userStatus" : 1,
    "completionStatus" : "IP",
}

I want to find those document where condition is something like this

(PROGRESS<100||(PROGRESS==100&&ASD not exists)).

James Z
  • 12,209
  • 10
  • 24
  • 44
Sumit Tiwari
  • 139
  • 1
  • 8
  • Possible duplicate of [Query MongoDB with $and and Multiple $or](https://stackoverflow.com/questions/40388657/query-mongodb-with-and-and-multiple-or) – Ashh Sep 24 '18 at 18:18

1 Answers1

1

This should get you going ($elemMatch):

db.collection.find({
    dataArray: {
        $elemMatch: {
            $or: [
                { Progress: { $lt: 100 } },
                { $and: [
                    { Progress: { $eq: 100 } },
                    { ASD: { $exists: false } }
                ]}
            ]
        }
    }
})

UPDATE based on your comment - this is even easier:

db.collection.find({
    $or: [
        { "dataArray.Progress": { $lt: 100 } },
        { $and: [
            { "dataArray.Progress": { $eq: 100 } },
            { "dataArray.ASD": { $exists: false } }
        ]}
    ]
})
dnickless
  • 10,733
  • 1
  • 19
  • 34