0

In case anyone else reads this, I've answered my own stupid question. The problem was that I was storing the value as an integer, but searching for it as a string. Pro tip: Store stuff as string values or; When querying for a integer, while using a variable, use parseInt(variable). It'll save you hours of pondering.

I'm simply trying to pass a variable along to mongodb (using mongojs), but it seems that it doesn't work if it's not a string or something.

I need to use the $gte and $lte to find some documents. The variable comes from app.get() /get/prods?startdate=1431057999826&enddate=1433563599832

The document look like this:

{ 
    "_id" : ObjectId("5571288438e80a073c0fc32c"), 
    "account" : "AC654164545-006",
    "uip" : 1217390010, 
    "ci" : "products", 
    "ts" : 1433479300668, 
    "guid" : "_upzghlayx", 
    "title" : "test_prod", 
    "type" : "car" 
}

This works just fine:

db.data.find({"account":'AC654164545-006', "ts": {$gte: 1431057999826, $lte: 1433563599832}});

But when I grab the query string, like so (the variables are the proper numbers, so I know they are correct):

var datestart = req.query.startdate;
var dateend = req.query.enddate;

and query using them:

db.data.find({"account":'AC654164545-006', "ts": {$gte: datestart, $lte: dateend}})

I get 0 results. I've also tried building the query beforehand.

var search = { "account":'AC654164545-006'};

search['ts'] = {$gte: datestart, $lte: dateend};

Again, 0 results. However if I switch to manual input without the variables, it works fine again.

search['ts'] = {$gte: 1431057999826, $lte: 1433563599832};

1 Answer 1

1

O.M.G.........

So, After reading my own question, and thinking about it, I realized that the document value "ts" is being stored as an integer.

So I changed:

search['ts'] = {$gte: datestart, $lte: dateend};

to

search['ts'] = {$gte: parseInt(datestart), $lte: parseInt(dateend)};

Ay ya, data types are hard.

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

Comments

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.