0

I have a schema like this.

const Company = new mongoose.Schema({
    profile: {
        name: String,
        email: String,
        password: String,
        description: String,
        phone: String,
        location: {
            address: String,
            latitude: Number,
            longitude: Number
        }
    },
    posts: Array,
    permission: { type: Number, default: 1 },
});

I have to search by distance, name, etc...

The problems is to search by distance.

How to make query for search by distance plus name, etc?

Thanks

Excalibur
  • 325
  • 2
  • 5
  • 15
  • 3
    You should save your coords in an [long, lat] Array and set a 2dsphere index for it. Then use $and operator with a geospatial query. – Getter Jetter Apr 27 '20 at 21:33
  • So I'm going to change the location like this. location: {type:"Point", coordinates: [lat,lng]}. And createindex({profile.location, '2dsphere'}). it will work? – Excalibur Apr 28 '20 at 09:02
  • Not sure if you're obliged to define the "Point" type here but you're on the right path. Keep in mind that mongoDB needs coordinates in [lng, lat] format (lng first). – Getter Jetter Apr 28 '20 at 09:20

0 Answers0