0

I wannt to create code like this:

var result = Attributes.find({
    attribute_name : {
    $exist : true,
    $in : [1]
 }
});

but programmatically, so i ceate code like this:

var genQuery = '{ "' + by + '" : { "$exists" : true, "$in" : [' + data + ']} }';
var result = Attributes.find(genQuery);

but I get error maximum call stack

because result of JSON.parse(genQuery)

{ _id: { '$exists': true, '$in': [ 1 ] } }

How to query in mongodb programmatically?

1 Answer 1

1

Your genQuery variable you declare is a String, but you cannot pass strings as selectors or modifiers in find() functions. You should create an Object to make it works:

var genQuery = {};
//use this notation to declare a new object key depending on a variable
genQuery[by] = {
  $exists: true, 
  $in: [1]
};

var result = Attributes.find(genQuery);
Sign up to request clarification or add additional context in comments.

6 Comments

I tested it in an existing collecting, it should work, do you get any error?
still same "maximum call stack"
when i console log it, same like this : { _id: { '$exists': true, '$in': [ 1 ] } } there '' surround $exist
remove the ' ' for $exists and $in, they are object keys not string.
As I show in my answer you don't need JSON.parse, just create an Object directly, instead of writing some kind of "object like string" and then parse it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.