0

I'm trying to find in a collection if there is already a session number, to avoid duplications. dadosORS.email and dadosORS.sessao (which is 3)come from a form. So when I do this:

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 {

I get result = undefined. If I change the query to

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

it lists my every occurence of the email:

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

So, how can I see if the dadosORS.sessao for that dadosORS.email already exists?

1 Answer 1

1

Just do an and query:

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

or can be expressed as

 collection.find( { $and: [ { email : dadosORS.email }, { sessao : dadosORS.sessao } ] } )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.