2

I have this query in mongoDB

var query = { "_id": new ObjectID(id)};
var items = {items:1};

warehouses.find(query, items,function (error, docs) {
    if (error) {
       error(error);
       return;
    }


    success(docs);
});

I want to get the items from a document by _id. This return me docs.items = undefined

But when i do:

warehouses.find(query, items).toArray(function (error, docs) {
            if (error) {
                error(error);
                return;
            }


            success(docs);
    });

Return me data:

0: Object
_id: ObjectID
items: Array[3]
0: Object
1: Object
2: Object
length: 3

How can i get the data with the first query? I want something like

1 Answer 1

4

There is a big difference. If you only expect one result, as in searching by the _id then use .findOne():

warehouses.findOne(query, items, function (error, doc) {

Otherwise you are returning a Cursor() object. At least so in the native driver and in some form or another in ODM's.

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.