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



我有收集,如下所示

  { 
        "_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)
}
.
.
.
.
.
.

我正在尝试检查该集合中是否存在电子邮件test41@gmail.com是否存在,如果我使用this.collection.findOne({ email: 'test41@gmail.com' });是否会在集合中查找所有记录,但是我如何通过使用_id字段将一个记录排除在集合中的一个记录以排除。p>这里的电子邮件存在于该集合的一个记录中 "_id" : ObjectId("58fe3768f997ca09d551c34e"),,但我想排除此记录并在集合中的其余记录中进行搜索。

不要使用findOne,请使用find查找所有出现。现在,如果您要排除一些文档,则可以通过关注

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

最新更新