1

Hi I have User Schema like this:-

var userSchema   = new Schema({
    name: {type: String, default: null},
    location: {
      type: { type: String },
      coordinates: [Number],
    },
    sentFriendRequests: [
        {type: Schema.Types.ObjectId, ref: 'user'}],
    receivedFriendRequests: [
        {type: Schema.Types.ObjectId, ref: 'user'}] 
});

It is working good for all the requirements. I am searching nearby users with this query:-

User.aggregate(
      [{
        $geoNear: {
          near: { type: "Point", coordinates: [ longitude , latitude ] },
          distanceField: "dist.calculated",
          num: 5,
          spherical: true
        }
      }], function(err, nearByUsers){
         console.log(nearByUsers);
      })

The above query is working very good But now i want to search only the users who are not my in my friends array and not in both sent and received friend request array.

1 Answer 1

1

Well assuming you have the user document (because you're using its coordinates) then just add a $match to filter out the users before the $geonear phase.

{ 
  $match: {
         $and: [
           { _id: {$nin: user.sentFriendRequests},
           { _id: {$nin: user.receivedFriendRequests}
          ]
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @tom slabbaert. It works but If i use it before $geonear then it gives a warning that $geonear is only valid as the first stage in the pipeline. Then i used it after the $geonear phase and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.