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

朴雅克·罗韩

谁能建议我如何才能从我的收藏中获得这些记录?在我的嵌入文档中有多个匹配的值。这是我的用户名收藏。

{
"_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,
}}

我在这里试图做的是只获取那些有像“星期三”和“星期四”这样的停车数据,只有我只想要停车数据。像这样。

{
"_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,
}}

为了得到这样的结果,我正在使用如下查询,但它没有按照我的意愿返回给我。下面是我正在使用的查询。

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

这是我正在使用的查询,它没有返回上述所需的结果,有人可以建议我获得结果吗

s7vr

您可以使用以下聚合。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章