I'm a little confused about what you're trying to achieve with that query.
The first object passed to .find specifies some search criteria, so you're fetching all users except the one with _id === userid.
The second object specifies which fields to include in the output, equivalent to a $project stage. You're creating a new field "friendsList", and pass in an expression {$ne:userid}.
First thing is, you'd have to pass an array to $ne, specifying which values to compare, something like:
{ $ne: [ '$friendsList._id', userid ] }
However, I'm not sure if that'll do what you expect, because that will return documents with a "friendsList" field and a Boolean as value.
Generally, I don't think it's possible to filter with $project. Assuming that you do want to filter, put it into the first object. Maybe you're looking for something like this?
User.find(
{ _id: { $ne: userid },
friendsList: { $ne: userid } }
)
That would return a list of users where
- _id !== userid (-> all users except one)
- userid is not present in the friendsList
So basically a list of people who aren't friends with Mr. Userid.
Hope that answered your question.
Pratik Zinjurde
aspiring web developer
hi can you tell me why this code is not working let user=await User.find({ _id: { $ne: userid } }, {friendsList : {$ne:userid}}).limit(3) im getting this error in postman { "ok": 0, "code": 2, "codeName": "BadValue", "name": "MongoError" }