MongoDb $ geoWithin查询子文档

时间:2017-06-19 06:11:52

标签: mongodb mongoose mongodb-query

我试图在子文档上进行geospacial查询。 所以我有一个如下所示的父模式:

var orderSchema = new Schema({
//other properties
venue: {
    type: Schema.Types.ObjectId,
    ref: 'Venue',
}
)};

在我孩子的场地内#39;我有架构:

location: {
    type: { type: String, default: "Point" },
    coordinates: [Number]
}

venueSchema.index({ "location": "2dsphere" });

我想要做的是直接查询我的订单架构以获得一些匹配条件,并在场地字段上执行$ geoWithin查询。 我没有问题这个查询直接在子文档类上执行所以我假设这一切都正常工作,索引设置正确

Venue.find({location : {
        $geoWithin: {
            $geometry: {
                type: "Polygon",
                coordinates: boundry.geometry.coordinates
            }
        }
    }})

当我尝试从父类查询这些子文档时,订单'我没有收到任何错误,只是没有匹配的结果:

Order.find({'venue.location' : {
        $geoWithin: {
            $geometry: {
                type: "Polygon",
                coordinates: boundry.geometry.coordinates
            }
        }
    }})

我没有从执行中得到任何反馈,所以我不确定这是不是支持还是我做错了。

我试图在没有运气的父项上索引子模式

orderSchema.index({ "venue.location": "2dsphere" })

修改 家长我查询:

{
  "_id": "5937687f533b9627a4f3afb5",
  "updated_at": "2017-06-07T10:01:26.832Z",
  "created_at": "2017-06-07T02:44:15.561Z",
  "venue": {
    "_id": "586c8da780e0733e124c7710",
    "updated_at": "2017-01-12T22:21:36.190Z",
    "created_at": "2017-01-04T05:52:39.364Z",
    "location_name": "Pizza Shop",
    "location_phone": "(555) 555-555",
    "hours": "11am-12pm Mon - Sat",
    "__v": 0,
    "location": {
      "coordinates": [
        -79.9318376,
        40.4586607
      ],
      "type": "Point"
    },
    "device": {
      "device_type": null,
      "device_id": null
    }
  },
  "receipt_image": null,
  "time": {
    "accepted": "2017-06-07T02:44:20.859Z",
    "picked_up": null,
    "ready_time": null,
    "delivered": null,
    "placed_at": "2017-06-07T02:38:44.556Z"
  },

在' boundry.geometry.coordinate'传递的特定坐标是:

[ [ [ -79.99745935177826, 40.43637059697235 ],
[ -79.99773685003227, 40.43631191597255 ],
[ -79.99802046433027, 40.436308499735446 ],
[ -79.99829929553387, 40.4363604795451 ],
[ -79.99856262831626, 40.436465857846585 ],
[ -79.99880034294631, 40.436620585010864 ],
[ -79.9990033041841, 40.4368187149598 ],
[ -79.99916371234308, 40.43705263367056 ],
[ -79.99927540302765, 40.43731335177827 ],
[ -79.99933408402745, 40.43759085003227 ],
[ -79.99933750026456, 40.43787446433027 ],
[ -79.9992855204549, 40.438153295533866 ],
[ -79.99918014215342, 40.43841662831626 ],
[ -79.99902541498913, 40.438654342946315 ],
[ -79.99882728504019, 40.438857304184104 ],
[ -79.99859336632944, 40.43901771234308 ],
[ -79.99833264822173, 40.43912940302765 ],
[ -79.93227424822173, 40.46004010302765 ],
[ -79.93199674996772, 40.46009878402745 ],
[ -79.93171313566972, 40.46010220026456 ],
[ -79.93143430446612, 40.4600502204549 ],
[ -79.93117097168373, 40.45994484215342 ],
[ -79.93093325705368, 40.45979011498914 ],
[ -79.93073029581589, 40.459591985040205 ],
[ -79.93056988765692, 40.45935806632944 ],
[ -79.93045819697234, 40.45909734822173 ],
[ -79.93039951597254, 40.45881984996773 ],
[ -79.93039609973543, 40.45853623566973 ],
[ -79.93044807954509, 40.45825740446614 ],
[ -79.93055345784657, 40.45799407168374 ],
[ -79.93070818501086, 40.45775635705369 ],
[ -79.9309063149598, 40.4575533958159 ],
[ -79.93114023367055, 40.457392987656924 ],
[ -79.93140095177826, 40.457281296972354 ],
[ -79.99745935177826, 40.43637059697235 ] ] ]

0 个答案:

没有答案
相关问题