【问题标题】:Mongoose Geospatial Query find all User Spheres which include a given pointMongoose 地理空间查询查找所有包含给定点的用户球体
【发布时间】:2017-08-23 11:12:04
【问题描述】:

我试图找到查询给定场景的正确方法。

我有一群用户,他们有一个位置 latlng 和一个用户定义的半径。 我有一个兴趣点,有一个位置 latlng。 现在我想找到所有用户他们的领域,包括兴趣点的纬度。

我现在告诉你如何查询,但这显然是错误的。

架构:

    const POISchema = new mongoose.Schema({
                                   poi_uuid: {type: String, required: true},
                                   category_uuid: {type: Number, required: true},
                                   category: {type: String, ref: 'SubCategory'},
                                   activity: {type: String, required: false},
                                   infos: {type: String, required: false},
                                   location: {
                                               coordinates: {type: [Number], index: '2d', required: false, default: []}
                                              }
                                           }, {usePushEach: true}); 


    const UserSchema = new mongoose.Schema({
                                   user_uuid: {type: String, required: true},
                                   helper_radius: {type: Number, required: true},
                                   address: {
                                               coordinates: {type: [Number], index: '2d', required: false, default: []}
                                             }
                                          }, {usePushEach: true});

查询:

User.find()
                    .and([
                        {
                            latlng: {
                                $near: [parseFloat(obj.coordinates[0]), parseFloat(obj.coordinates[1])],
                                $maxDistance: parseFloat(20 / 111.2)
                            }
                        },
                        {'_id': {'$ne': obj._id}},
                        {'categories': obj.category_uuid}
                    ])
                    .exec(function (err, users) {
                        if (err) {
                            console.log(err);
                            return res.send(500, JSON.stringify(err))
                        }
                        return res.send({users: users})
                    })

我知道我可能必须使用 $geoWithin 或 $geoIntersects。但我真的不知道怎么读,我读的就像我能找到的一切。我什至没有找到如何为具有定义半径的 2d 球体声明带有 mongoose 的模式字段。也许有人可以指出我正确的方向或有一些有用的链接。

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query geospatial


    【解决方案1】:

    我认为您正在寻找这样的东西:

    var poi; // this is the point of interest for which I want to find the users nearby    
    User.aggregate(
        [
            { "$geoNear": {
                "near": poi.location.coordinates,
                "distanceField": "distance"
            }},
            { "$redact": {
                "$cond": {
                    "if": { "$lt": [ "$distance", "$helper_radius" ] },
                    "then": "$$KEEP",
                    "else": "$$PRUNE"
                }
            }}
        ],
        function(err,results) {
           // read the response
        }
    );
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2019-09-23
    • 1970-01-01
    • 2014-06-04
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多