I have this piece of code :
db.collection('coders', function(err, collection) {
collection.find(toFind).toArray(function(err, items) {
res.send(items);
});
where toFind is something like {"position":2,"$or":[{"position":{"$lt":20}},{"name":"whatever"}]}
It is a String, so the previous code doesn't execute, because it needs an object. I already know, that I can create object from String like
var obj={}
obj[key] = {value}
But how can I create an object without key ?
To convert toFind from a string into an object that you can pass into find, use JSON.parse:
toFind = JSON.parse(toFind);
Everything in JavaScript is an object. However, what you apparently want is a simple variable:
obj[key] = value;
The object you posted above would be defined exactly like this:
var obj = {
"position": 2,
"$or": [{
"position": {
"$lt": 20
}
}, {
"name": "whatever"
}]
};