Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all except one in mongodb

I want to skip one user particular by using _id and display all others users, I am trying below code its not working:

db.user.find(
    { _id: { $nin: ["5848e9ecaec0f31372816a26"] } },
    { username: 1 }
).pretty()
like image 965
GsMalhotra Avatar asked Dec 19 '16 13:12

GsMalhotra


2 Answers

Use ObjectId for _id :

db.user.find( { _id: { $nin: [ObjectId("5848e9ecaec0f31372816a26")] } } )
like image 151
P.Madhukar Avatar answered Oct 02 '22 16:10

P.Madhukar


Short answer: Use $ne for one user db.user.find( {_id:{$ne:"5848e9ecaec0f31372816a26"} }) .

like image 23
lomboboo Avatar answered Oct 02 '22 15:10

lomboboo