简体   繁体   English

检查mongoDb集合中是否存在特定字段,但不包括一条记录

[英]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' }); 我正在尝试检查此集合中是否存在电子邮件test41@gmail.com ,如果我使用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. 将查找集合中的所有记录,但是如何使用_id字段作为排除对象的引用来排除集合中的一条记录。

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. 在这里,该电子邮件存在于具有"_id" : ObjectId("58fe3768f997ca09d551c34e"),的集合的一个记录中"_id" : ObjectId("58fe3768f997ca09d551c34e"), ,但是我想排除该记录并在集合的其余记录中进行搜索。

Don't use findOne , use find to find all occurrences. 不要使用findOne ,而要使用find查找所有事件。 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")}}]})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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