Node Mongo - 找到多个参数

问题描述 投票:0回答:1

如果已经有会话号码,我试图在集合中找到,以避免重复。 dadosORS.email和dadosORS.sessao(这是3)来自一个表格。所以当我这样做时:

mongoClient.collection('registosORS', function(err,collection){
  collection.find({email:{$eq:dadosORS.email}},{sessao:{$eq:dadosORS.sessao}}).toArray(function(err,result){

                    try{
                    console.log(result);
                    }catch (err){
                      console.log(err);                          
                    }

                      if(result){
                        // callback(false)
                        return
                      } else {

我得到结果=未定义。如果我将查询更改为

 collection.find({email:dadosORS.email},{sessao:dadosORS.sessao}).toArray(function(err,result){

它列出了我每次发生的电子邮件:

      [ { _id: 5a37b4c3da53ff1e825f94b4, sessao: '1' },
{ _id: 5a37b4e6da53ff1e825f94b6, sessao: '1' },
    { _id: 5a37b57ce500ca1ea5522e22, sessao: '2' } ]

那么,我怎么看看dadosORS.email的dadosORS.sessao是否已经存在?

node.js mongodb
1个回答
0
投票

只需做一个and查询:

 collection.find( { email : dadosORS.email, sessao : dadosORS.sessao } )

或者可以表达为

 collection.find( { $and: [ { email : dadosORS.email }, { sessao : dadosORS.sessao } ] } )
© www.soinside.com 2019 - 2024. All rights reserved.