简体   繁体   English

mongo 查询 $near 总是 0 结果

[英]mongo query $near always 0 results

My search Position is the same as the position in the database.我的搜索位置与数据库中的位置相同。 The result is an empty array.结果是一个空数组。 I expected to get the one element from database, because the distance between both locations is 0.我希望从数据库中获取一个元素,因为两个位置之间的距离为 0。

Mongo Doku $near Mongo Doku $near

Query to find all nearest查询以查找所有最近的

Request.find({
            address: {
                location: {
                    $near: {
                        $geometry: {
                            type: 'Point' ,
                            coordinates: [8.4821159,49.4705199],
                        },
                        $maxDistance: 10000,
                        $minDistance: 0,
                    },
                },
            },
        })

Mongoose Model猫鼬模型

Edit (add): this.request.index({'address.location': '2dsphere'});编辑(添加):this.request.index({'address.location': '2dsphere'});

  import mongoose from 'mongoose';

const ObjectId = mongoose.Schema.Types.ObjectId;
import {RequestMiddleware} from './RequestMiddleware';

class Request extends mongoose.Schema {
    public request: mongoose.Schema;

    constructor() {
        const RequestSchema = {
            title: {
                type: String,
                required: true,
            },
            description: {
                type: String,
                required: true,
            },
            category: {
                type: ObjectId,
                ref: 'Category',
                required: true,
            },
            created_by: {
                type: ObjectId,
                ref: 'User',
                required: true,
            },
            address: {
                location: {
                    type: {
                        type: String,
                        enum: ['Point'],
                        default: 'Point',
                        required: true,
                    },
                    coordinates: {
                        type: [Number],
                        default: [0, 0],
                        required: true,
                    },
                },
                plz: {
                    type: String,
                    required: false,
                },
                city: {
                    type: String,
                    required: false,
                },
                street: {
                    type: String,
                    required: false,
                },
                street_nr: {
                    type: String,
                    required: false,
                },
            },
            time_end: {
                type: Date,
                required: false,
            },
            confirmed_helper: {
                type: ObjectId,
                ref: 'User',
            },
            helper: [{
                helperId: {
                    type: ObjectId,
                    ref: 'User',
                },
                offer_text: {
                    type: String,
                },
            }],
        };
        const request = super(RequestSchema, {
            timestamps: {
                createdAt: 'created_at',
                updatedAt: 'updated_at',
            },
        });
        this.request = request;
        this.request.index({'address.location': '2dsphere'});
        this.request.plugin(RequestMiddleware);

        return this.request;
    }
}

export default mongoose.model('Request', new Request());

Database:数据库:

数据库

You need two things:你需要两件事:

2dspere index (probably you already have it): 2dspere 索引(可能你已经有了):

db.col.createIndex( { "address.location" : "2dsphere" } )

and to modify your query so that it uses the dot notation instead of nested object:并修改您的查询,使其使用点表示法而不是嵌套对象:

let result = await Request.find({
        'address.location': {
            $near: {
                $geometry: {
                    type: 'Point',
                    coordinates: [8.4821159, 49.4705199]
                },
                $maxDistance: 10000,
                $minDistance: 0
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM