Alexander Ceballos
Alexander Ceballos

Reputation: 760

$all mongodb node js

I am implementing a search by keywords with mongodb and node JS, but I see that there is the operator $all in Mongo that selects the documents where the field holds an array and contains all elements.

Here is my source code with node JS

exports.find = function(req, res) {
    var b=req.params.search;
    var query = {};
    var cadsrch = b.split(' ');
    var l = cadsrch.length;
    var i = 0;
    for (i = 0; i < l; i++) {
        if(cadsrch[i]!=''){
          query[i]=new RegExp('^'+cadsrch[i], 'i');
        }
    }

    db.collection('publicacion', function(err, collection) {
        collection.find({tags: {'$all':query}},{title:true,content:true}).limit(5).toArray(function(err, items) {
            res.jsonp(items);
        });
    });
};

The above source does not work, but this query works in Mongo->

db.publication.find({tags:{$all:['chevrolet','car']}})

and the strange is with '$in' instead '$all' works, but it would be useful to work with '$all' to implement exact searches

Upvotes: 3

Views: 420

Answers (1)

Gates VP
Gates VP

Reputation: 45297

You have defined your query as an object {}. However, the example you use in the mongo shell has an array [].

I know they are "kind of" the same thing, but it's probably worth making these the same type.

Upvotes: 1

Related Questions