2

I have data something like this

{
  "_id": ObjectId("52ed12c144aecc4bf004d0b6"),
  "active": true,
  "name": "woslo",
 "specialDays": [
  {
  "_id": ObjectId("5f0576196198a715b0a72c14")
  "status": true
  "date": 2020-07-08T04:00:00.000+00:00
  },
  {
  "_id": ObjectId("5f05a3726198a715b0a72c94")
  "status": false
  "date": 2020-07-09T04:00:00.000+00:00
  }
 ]
}

I want to fetch records using this query

   db.serviceProviders.aggregate([
    {
      $match: {
            specialDays: {
              $elemMatch: {
                $or: [
                  {
                    $and: [
                      {
                        date:  model.date // 2020-07-09T06:00:00.000Z
                      },
                      {
                        status: true
                      }
                    ]
                  },
                  {
                    date: {
                      $ne:  model.date //2020-07-09T06:00:00.000Z
                    }
                  }
                ]
              }
            }
          }
        }
  ]);

Scenario is : if date is present in specialDays array and the status should be true , or date should not be in specialDays Object's array then fetch this record. But every time it's fetching the same record that is above even status is false or date is present in array. Would you please help me how to figure it out, I tried a lot of queries in Mongo compass aggregation with ISODate('2020-07-08') but still not working. Thank you Happy Coding.

1 Answer 1

2

Problem is with your $ne condition. If status is false, then your $ne condition is true. Since it is logical OR, you are getting the output.

How about this?

db.collection.aggregate([
  {
    $match: {
      specialDays: {
        $elemMatch: {
          $or: [
            {
              $and: [
                {
                  date: new Date("2020-07-09T04:00:00.000+00:00")
                },
                {
                  status: true
                }
              ]
            },
            {
              date: {//Changes here
                $gte: new Date("2020-07-09T06:00:00.000+00:00"),
                $lte: new Date("2020-07-09T23:59:59.000+00:00")
              }
            }
          ]
        }
      }
    }
  }
])

OR

this

Another reason for your $ne condition is true because it satisfies your first array element in specialDays array

db.collection.aggregate([
  {
    $match: {
      specialDays: {
        $elemMatch: {
          $or: [
            {
              $and: [
                {
                  date: new Date("2020-07-09T04:00:00.000+00:00")
                },
                {
                  status: true
                }
              ]
            },
            {
              $and: [
                {
                  date: {
                    $ne: new Date("2020-07-09T04:00:00.000+00:00")
                  }
                },
                {
                  status: false
                }
              ]
            }
          ]
        }
      }
    }
  }
])
Sign up to request clarification or add additional context in comments.

2 Comments

Wonderful, thanks to you man :), it solved my problem.
Glad, it helped. Happy coding @TanzeelSaleem

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.