Reputation: 12326
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
Upvotes: 2
Views: 617
Reputation: 151132
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.
Upvotes: 4