Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 查询以获取多个数组中不存在的数据_Mongodb_Mongodb Query - Fatal编程技术网

Mongodb 查询以获取多个数组中不存在的数据

Mongodb 查询以获取多个数组中不存在的数据,mongodb,mongodb-query,Mongodb,Mongodb Query,嗨,我有这样的用户模式:- var userSchema = new Schema({ name: {type: String, default: null}, location: { type: { type: String }, coordinates: [Number], }, sentFriendRequests: [ {type: Schema.Types.ObjectId, ref: 'user'}],

嗨,我有这样的用户模式:-

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'}] 
});
它可以满足所有要求。我正在使用此查询搜索附近的用户:-

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

上面的查询工作得很好,但现在我只想在my friends数组中搜索不属于我的用户,而不是在sent和received friend request数组中搜索

假设您拥有用户文档(因为您使用的是它的坐标),那么只需添加一个$match,在$geonear阶段之前过滤掉用户

{ 
  $match: {
         $and: [
           { _id: {$nin: user.sentFriendRequests},
           { _id: {$nin: user.receivedFriendRequests}
          ]
    }
}

谢谢你@tom slabbaert。它可以工作,但是如果我在$geonear之前使用它,那么它会给出一个警告,$geonear仅在管道中的第一个阶段有效。然后我在$geonear阶段之后使用它,它就可以工作了。