0

I am currently testing a sample database and my query wasn't returning anything until I placed the (+) character prior to the variable holding the integer value in my $gt query operator. Please shed some insight.

var mongo = require('mongodb').MongoClient;
var age = process.argv[2];
// console.log(process.argv);

var url = 'mongodb://localhost:27017/learnyoumongo';

mongo.connect(url, function(err, db){
    if (err) throw err;
    var parrots = db.collection('parrots');
    parrots.count({
        "age": {$gt: +age} // What does (+) do?
    }, function(err, data){
        if (err) throw err;
        console.log(data);
        db.close();
    });
});

1 Answer 1

2

Because the age field in your mongodb is of integer type, using the $gt comparison operator only works when you are comparing integers. The + sign in this case is a unary operator which returns the number representation of the object process.argv[2]. It can convert string representations of integers and floats, as well as the non-string values true, false, and null.

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.