0

I want to traverse and find the current location of users by near. But can not query within users.currentlocation.

My schemas;

const UserSchema = new Schema({
    name: { type: String, default: '' },
    surname: { type: String, default: '' },
    email: { type: String, default: '', },
    salt: { type: String, default: '' },
    hashed_password: { type: String, default: '' },
    interested: { type: [String] },
    about: { type: String },
    currentcheckin: {type:Schema.Types.Mixed, ref: 'Checkin', index:'2d'},
    createdAt: {type:Date, default:Date.now},
    searchable: {type:Boolean, default:false}   
});

const CheckInSchema = new Schema({
    user: { type: Schema.Types.ObjectId, ref: 'User' },
    createdAt: { type: Date, default: Date.now },
    loc: {
        type: [Number],
        index: '2d',
        default: [0, 0]
    },
    range: { type: Number, default: 0 },
    listable: {type:Boolean, default:true},
});

My query structure is;

With this query, i want to get the users who is near each other. I do not want to store the current locations of users in a seperate collection or marking the documents as currentLocation. I want to traverse only users not all the checkins.

exports.searchable = async(function*(req,res,next){
    req.user.searchable = req.body.searchable;
    //const scanneds = undefined;
    try{
        const checkin =  Checkin.load(user.currentcheckin);
        var coords = [];
        coords[0] =  23.600800037384033,
        coords[1] =   46.76758746952729;
        var currentcheckins =  yield User.find({
            'currentcheckin.loc':
            {
                $near: coords,
                $maxDistance: 100

        }});


        //yield req.user.save();
        return respond(res,currentcheckins,200);
    }catch(err){
        //return next(err);
    }



});

But query holds and not responding, thanks,

L'lynn
  • 167
  • 1
  • 13
  • You have already asked this question and have already been pointed at the relevant existing answers. Your "geospatial" data is "referenced" in a different collection, therefore you use either the nested populate syntax or the `$lookup` operator for the related data as is shown in [Querying after populate in Mongoose](https://stackoverflow.com/questions/11303294/querying-after-populate-in-mongoose). Of course the alternate is to do the "geospatial" query **first** and do the `$lookup` on the reverse to the "parent" `User`. Of course the other alternative is to "embed" in the first place. – Neil Lunn Nov 11 '17 at 23:39
  • But overall, the question is already answered aside from the fact that you are simply posting the same question again in which you were already pointed to those same answers. Duplicates are not a "slight on you", but simply pointing you to where the question has already been answered, but you may have not been aware of the answer. Now you know. – Neil Lunn Nov 11 '17 at 23:40
  • can not use $near on match. And can not use geoNear after the lookup. geoNear must be first on the pipeline. Can you help me? – L'lynn Nov 12 '17 at 11:16

0 Answers0