mongo geoNear with lookup aggregation

时间:2017-01-28 19:50:15

标签: mongodb

我有收藏地点,有地点位置。我还有一个有placeId字段的集合席位。

我要做的是选择带位置的席位(作为外键),并在places.location字段上使用名为location(包含lat和long)的参数进行geoLocation。

现在我有了这个

fill_database.js

conn = new Mongo();

db = conn.getDB('test');

db.places.remove({});
db.seats.remove({});

db.places.insert({
    placeName: '84th NYC',
    placeAddress: '083 01 Sabinov 9. maja',
    location: {
        type: 'Point',
        coordinates: [49.097968, 21.105365],
    },
});

lastPlace = db.places.find()[0];
db.seats.insert({
    seatName: 'Seat 1',
    description: 'Located in the corner of the room very quiet and comfortable',
    type: 'solo',
    pictures: ['http://lorempixel.com/400/200/'],
    placeId: lastPlace._id,
});
db.seats.insert({
    seatName: 'Seat 21',
    description: 'Located by the window, bright setting, very quiet and comfortable',
    type: 'solo',
    pictures: ['http://lorempixel.com/400/200/'],
    placeId: lastPlace._id,
});


db.places.createIndex({location: '2dsphere'});

用作$ mongo fill_database.js 这使用Mongoose:

seats.js

self.aggregate([
    {$geoNear: {near: {type: 'Point', coordinates: [location.lat, location.long]}, distanceField: 'places.dist', maxDistance: 100, includeLocs: 'places.location', spherical: true}},
    {$lookup: {from: 'places', localField: 'placeId', foreignField: '_id', as: 'place'}},
]

0 个答案:

没有答案