Search code examples
feathersjs

$ne not working in MongoDB query


My MongoDB query isn't filtering by $ne on the FeathersJS client plugin. Location/$near is working.

 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
                }

Solution

  • Normally in Mongo to compare ObjectIDs you need to wrap your value (user._id) with ObjectId, importing it from Mongo.

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

    The reason is that an objectId is not a string internally, I guess. :)