0

I have a mongoDB collection which contains a list of documents which contain (among other items) a time stamp and an address for each different type of device. The device address is in hexidecimal (eg. 0x1001, 0x2001, 0x3001, etc.).

On the server side, I'm trying to query the collection to see what documents exists within a certain date range, and for a list of device

Collection.find(
    {"addr": data.devices.Controls, "time": {$gte:d0, $lte:d1}},{},
    function (err, docs) {
        if( err|| !docs) console.log("No data found");
        else {
            //I've simplified the code here...
        }
    }
);

d0 and d1 are my start and end dates... and the data.devices.Controls is a list of device addresses. If I add the line:

console.log("Controls: " + JSON.stringify(data.devices.Controls));

I can see on the server side that it prints out a list of addresses that I'm looking for (the actual print statement looks like: Controls: ["0x1001", "0x2001", "0x3001"].)

However, this find statement doesn't seem to return any data from the query. There's no error (as I don't see the "No Data Found" message)... It just doesn't seem to return any data. What's strange is that if I specify a specific element out of the Controls array (something like data.devices.Controls[0]...), then it works fine. I can specify any element in the array and it works... but by passing an entire array in the argument, it doesn't seem to work. Does anyone know why this happens (and how to fix it)?

1 Answer 1

2

You need to use the $in operator to match against an array of values; like this:

Collection.find(
    {"addr": {$in: data.devices.Controls}, "time": {$gte:d0, $lte:d1}}, ...
Sign up to request clarification or add additional context in comments.

1 Comment

That was it! I had no idea about the $in operator. I'm glad I asked the question, and even more glad to have a quick answer. Thanks again.

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.