nodejs - mongodb - how to find all where a != b?

12,021

Solution 1

The answer can be found here

nodejs - mongodb - how to find all where a != b?

Solution 2

You can use the not equals '$ne' for comparation

collection.findAll({a: {'$ne':b }}, function(err, cursor) {});

Check out this advanced queries manual page for more detailed explanations.

Share:
12,021
angry kiwi
Author by

angry kiwi

Updated on June 13, 2022

Comments

  • angry kiwi
    angry kiwi almost 2 years

    this is how I imagine it

        //b is variable
        collection.findAll({a:'!'+b}function(err, cursor) {
    
        });
    

    What the correct way to that query, find all result where a != b ?

  • angry kiwi
    angry kiwi almost 13 years
    I tested the code, it will fetch everything that equals a. :(
  • Atzoya
    Atzoya almost 13 years
    Try the same query in your mongodb console window like this: db.nameOfCollection.find({a: {'$ne':b}}). It should return the documents from the collection where a != b. If it does, the problem is elsewhere and not in the code snippet from your question
  • Atzoya
    Atzoya almost 13 years
    also try using collection.find() instead of findAll()