嵌入式文档上的Mongodb node.js查询具有多个值的单个字段。

时间:2018-09-13 13:50:55

标签: node.js mongodb mongoose aggregation-framework

有人可以建议我如何仅从收藏夹中获取那些记录吗?在我的嵌入式文档中有多个匹配的值。这是我的收藏集,具有名称用户。

{
"_id": "5b86fd75d595a923452366d7",
"name": "Mike I",
"email": "ppp@ppp.com",
"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,
}}

我在这里要做的是仅获取那些停车日为“ Wednesday”和“ Thursday”的停车数据,而我只是想要该停车数据。这样。

{
"_id": "5b86fd75d595a923452366d7",
"name": "Mike I",
"email": "ppp@ppp.com",
"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,
}}

并获得这样的结果,我正在使用查询如下,但它没有像我想要的那样返回我。下面是我正在使用的查询。

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"
                });
            }
        });

这是我正在使用的查询,它没有返回上面想要的结果,有人可以建议我一些东西来获取结果

1 个答案:

答案 0 :(得分:1)

您可以使用以下汇总。

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"
      });
  }
});