Pazuzu
Pazuzu

Reputation: 303

MongoDB aggregation syntax

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();
    });
});

Upvotes: 0

Views: 109

Answers (1)

chridam
chridam

Reputation: 103335

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.

Upvotes: 2

Related Questions