简体   繁体   中英

Check if particular field exists in mongoDb Collection,excluding one record

I have collection, as shown below

  { 
        "_id" : ObjectId("58fe3768f997ca09d551c34e"), 
        "firstName" : "arbar", 
        "lastName" : "ame", 
        "email" : "test41@gmail.com", 
        "phone" : "9966589965", 
        "password" : "$2a$10$pgRWb8Db385A5BbicEDJ2erHuUQsAIVmjqVuccXj7x.1iptdY/z7a", 
        "team_id" : ObjectId("58ef6d0a11c37915acaf7c9b"), 
        "activated" : false, 
        "accessLevel" : NumberInt(6)
    }
    { 
        "_id" : ObjectId("58fe37c2f997ca09d551c350"), 
        "firstName" : "Abrar Ahmed", 
        "lastName" : "asdf", 
        "email" : "test42@gmail.com", 
        "phone" : "9966158845", 
        "password" : "$2a$10$y3hPjuHeq0HVyukTnGCRT.k5xfSUH0z/mdGR8n7Gu09f7A7Z20bV6", 
        "team_id" : ObjectId("58ef6d0a11c37915acaf7c9b"), 
        "activated" : false, 
        "accessLevel" : NumberInt(6)
}
.
.
.
.
.
.

I am trying to check if email test41@gmail.com exists in this collection, if I use this.collection.findOne({ email: 'test41@gmail.com' }); will look for all the records in a collection but how would I exclude one record in a collection by using _id field as reference to exclude.

Here the email is exists in one record of the collection with "_id" : ObjectId("58fe3768f997ca09d551c34e"), , but I want to exclude this record and search in rest of the records in a Collection.

Don't use findOne , use find to find all occurrences. Now if you want to exclude some document you can do by following

db.collection.find({$and: [{"email": "test41@gmail.com"}}, {"_id": {$ne: ObjectId("58fe3768f997ca09d551c34e")}}]})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM