【发布时间】:2017-10-20 10:14:46
【问题描述】:
我正在尝试动态构建查询,我可以构建它并将其复制并直接粘贴到控制台中,它可以工作。
似乎该查询未被识别为我被卡住的对象:(
有没有办法可以将query字符串转换成mongodb可以识别的对象?
这用于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);
});
});
【问题讨论】:
标签: node.js mongodb aws-lambda