0

I'm using the MongoDB $and in combination with $lt and $gt like this:

  var queryString0 = 'data.' + req.body.data[0].keyname;

  console.log(queryString0);
  //Prints 'data.price'

  Company.find({ $and : [{ queryString0 : { $lt : 200, $gt : 100}}]}, {name: 1, queryString0 : 1} , function(err, result) {
    if (err) {
      throw err;
    }

    res.send(result);

  });

The problem is that this gives me no results while I use the queryString0 to retrieve the data, however, it gives me the desired results if I use a static string like this:

  var queryString0 = 'data.screener.' + req.body.data[0].keyname;

  console.log(queryString0);
  //Prints 'data.price'

  Company.find({ $and : [{ 'data.price' : { $lt : 200, $gt : 100}}]}, {name: 1, 'data.price' : 1} , function(err, result) {
    if (err) {
      throw err;
    }

    res.send(result);

  });

This doesn't make any sense to me, as the same query-string should be provided in both cases.

Please help me figuring out what is wrong with my code.

EDIT: The reason I used $and is because I will also need to check if several fields match my other conditions, like this:

  var queryString0 = 'data.screener.' +req.body.data[0].keyname;
  var queryString1 = 'data.screener.' +req.body.data[1].keyname;

  Company.find({ $and : [
    {queryString0 : {$lt : req.body.data[0]["max"], $gt : req.body.data[0]["min"]}},
    {queryString1 : {$lt : req.body.data[1]["max"], $gt : req.body.data[1]["min"]}}
  ] },{ name : 1, queryString0 : 1, _id : 0 }, function(err, result) {
    if (err) {
      throw err;
    }

    res.send(result);

  })
2
  • 1
    Why are you using $and when you only have one filter criteria in the array you assign to it? Commented Aug 26, 2015 at 7:26
  • Acutally astounds me the number of times people reach for $and rather than accept that all conditions are "and" arguments anyway, and yet have no problems writing { $gt: 100, $lt: 200 } which is in itself "yet another form of implicit and", as multiple arguments to the "right" of the key means exactly that. Commented Aug 26, 2015 at 7:33

1 Answer 1

2

And you started of with the right concept my constructing something in code, but forgot that JavaScript "stringifies" object keys as well. So there is a different notation for that.

Plus you also not no need $and here at all:

var queryString0 = 'data.screener.' + req.body.data[0].keyname;

var query = {};
query[querystring0] = { $lt : 200, $gt : 100};

var projection = { name: 1 };
projection[querystring0] = 1;

Company.find(query,projection,function(err,result) {
   // work in here
});

So the "bracketed" notation is how you use variable contents as the name of a key. And "all" MongoDB query arguments are "and" conditions anyway, so you would rarely need to wrap them with an $and, unless you need "two" or more conditions for the same field.

Sign up to request clarification or add additional context in comments.

3 Comments

I should have been more specific with why I used $and, could you please edit your answer to the additional information?
@martin Frankly no. The usage of $and here in iscorrect and should not be encouraged. You only ever need $and explicitly when you need to specifiy multiple different conditions for the same field. And even then, most of the time it should be written in that case just as the $gt and $lt operators are being used. That too is another "and" condition. Not that it changes the base of this answer anyway. As no matter what the structure, this is how you dynamically name a key.
@martin It is also considered highly rude to change your question to ask another or different question. If you learned something here then it is already solved as "you do not need $and". If you have additional questions still then "Ask another question".

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.