$ne 在 MongoDB 查询中不工作

$ne not working in MongoDB query

我的 MongoDB 查询未按 FeathersJS 客户端插件上的 $ne 进行过滤。 Location/$near 正在工作。

 app.service('users').find({
                query: {
                    location: {
                        $near: {
                            $geometry: {type: "Point", coordinates: [user.location.coordinates[0], user.location.coordinates[1]]},
                            $minDistance: 0,
                            $maxDistance: 20000
                        }
                    },
                    _id: {
                        $ne: user._id
                    },
                    profileSetup: true
                }

通常在 Mongo 中比较 ObjectID,您需要用 ObjectId 包装您的值 (user._id),从 Mongo.

导入它
const ObjectID = require("mongodb").ObjectID

 app.service('users').find({
   query: {
     location: {
       //... geo query here
       _id: {
          $ne: ObjectID(user._id)
       },
       profileSetup: true
     }

原因是objectId在内部不是字符串,我猜。 :)