简体   繁体   中英

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.

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

  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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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