Saturday 20 May 2017

MongoError: query selector must be an object

I am trying to dynamically build a query, I can build it and copy and paste it straight into the console and it works.

It seems that the query is not recognised as an object which is where I am stuck:(

Is there a way that I can convert the query string into an obbject that mongodb can recognise?

This is used in nodejs 6.10

var string = "tag1,tag2".split(",").map(function (a) { return new RegExp('^' + a) }).join(", ");

var query = '{ "optionsSearch": { "$all": [ ' + string + ' ] } }';

// this is my desired query below, if I copy and paste this straight into the query it works? 

console.log(query);  ///<- { "optionsSearch": { "$all": [ /^tag1/, /^tag2/ ] } } 

mongodb.MongoClient.connect('mongodb://*******:*****@aws-eu-west-1-portal#####', function (err, db) {

console.log("Connected to mongo");

var minidealers = db.collection('collection1');

minidealers.find(query) //<- FAILS MongoError: query selector must be an object
    .limit(100)
    .toArray(function (err, items) {
        console.log(items);
    });
});



via Jason

No comments:

Post a Comment