0

Can anyone suggest me how can I get only those records from my collection? which is having multiple matched Values in my embedded document. here is my collection having name users.

{
"_id": "5b86fd75d595a923452366d7",
"name": "Mike I",
"email": "[email protected]",
"status": true,
"role": "seller",
"parking_space": [
{
    "_id": "5b8cc33846e5360e741cae77",
    "parking_name": "New Parking",
    "street_address": "700 broadway",
    "opening_days_and_timings": [
    {
        "day": "Monday",
        "opening_time": "09:00",
        "closing_time": "10:00",
        "_id": "5b9119c93f7bc41306745a57"
    },
    {
        "day": "Sunday",
        "opening_time": "22:30",
        "closing_time": "23:30",
        "_id": "5b9119c93f7bc41306745a58"
    },
    {
        "day": "Tuesday",
        "opening_time": "11:00",
        "closing_time": "23:59",
        "_id": "5b9119c93f7bc41306745a59"
    }],
    "location":
    {
        "type": "Point",
        "coordinates": [-117.15833099999998, 32.7157529]
    },
    "status": true,
},
{
    "_id": "5b8e1df246e5360e741cae8a",
    "parking_name": "Gupta's parking",
    "street_address": "IT Park Rd, Phase - I",
    "opening_days_and_timings": [
    {
        "day": "Wednesday",
        "opening_time": "00:00",
        "closing_time": "23:59",
        "_id": "5b9119df3f7bc41306745a60"
    },
    {
        "day": "Thursday",
        "opening_time": "00:00",
        "closing_time": "23:59",
        "_id": "5b9119df3f7bc41306745a61"
    }],
    "location":
    {
        "type": "Point",
        "coordinates": [76.84613349999995, 30.7259198]
    },
    "status": true,
}}

What I am trying to do here is to get only those parking data which is having days like "Wednesday" and "Thursday" only i just want that parking data. like this.

{
"_id": "5b86fd75d595a923452366d7",
"name": "Mike I",
"email": "[email protected]",
"status": true,
"role": "seller",
"parking_space": [
{
    "_id": "5b8e1df246e5360e741cae8a",
    "parking_name": "Gupta's parking",
    "street_address": "IT Park Rd, Phase - I",
    "opening_days_and_timings": [
    {
        "day": "Wednesday",
        "opening_time": "00:00",
        "closing_time": "23:59",
        "_id": "5b9119df3f7bc41306745a60"
    },
    {
        "day": "Thursday",
        "opening_time": "00:00",
        "closing_time": "23:59",
        "_id": "5b9119df3f7bc41306745a61"
    }],
    "location":
    {
        "type": "Point",
        "coordinates": [76.84613349999995, 30.7259198]
    },
    "status": true,
}}

and to get result like this i am using query as follow but it is not returning me the as i want. below is the query i am using.

User.aggregate([{
            "$match": {
                "parking_space.location": {
                    "$geoWithin": {
                        "$centerSphere": [
                            [parseFloat(req.body.long), parseFloat(req.body.lat)], 7 / 3963.2
                        ]
                    }
                },
                $and : [ { "parking_space.opening_days_and_timings.day" : 'Thursday' }, { "parking_space.opening_days_and_timings.day" : 'Wednesday' } 
                "parking_space.opening_days_and_timings.day": getDayOfWeek(req.body.date)
            }
        }, {
            "$unwind": "$parking_space"
        }, {
            "$match": {
                "parking_space.location": {
                    "$geoWithin": {
                        "$centerSphere": [
                            [parseFloat(req.body.long), parseFloat(req.body.lat)], 7 / 3963.2
                        ]
                    }
                },
            }
        }], function(err, park_places) {
            if (err) {
                return res.send({
                    data: err,
                    status: false
                });
            } else {
                return res.send({
                    data: park_places,
                    status: true,
                    msg: "Parking data according to location"
                });
            }
        });

This is the query i am using which is not returning the above desired result can someone suggest me something to get the result

1
  • For 1 user at a time? Commented Sep 13, 2018 at 14:11

1 Answer 1

1

You can use below aggregation.

User.aggregate([
  {
    "$match": {
        "parking_space.location": {
            "$geoWithin": {
                "$centerSphere": [
                    [parseFloat(req.body.long), parseFloat(req.body.lat)], 7 / 3963.2
                ]
            }
        },
        {"parking_space.opening_days_and_timings.day" :{"$in":['Thursday', 'Wednesday', getDayOfWeek(req.body.date)]}}
    }
  },{ 
    "$addFields": {
          "parking_space": {
            "$filter": {
              "input": "$parking_space",
              "cond": {"$setIsSubset":[['Thursday', 'Wednesday', getDayOfWeek(req.body.date)], "$$this.opening_days_and_timings.day"]}
            }
          }
      }
  }
], 
function(err, park_places) {
  if (err) {
      return res.send({
          data: err,
          status: false
      });
  } else {
      return res.send({
          data: park_places,
          status: true,
          msg: "Parking data according to location"
      });
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I have tried this solution but it did not work for me. it is also giving me other documents which are having only one day also.
Yw. Can you please provide me an example where it didn't work with supplied input for the query ?

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.