This is the code:
usermodel.findOne({"imagen._id": req.params.id }, {imagen: 1}, function (err, imagen){
if(err) throw err;
console.log(req.params.id + '\n');
console.log(imagen);
res.send(imagen);
});
And this is the Schema:
var userschema = new mongoose.Schema({
user: String,
imagen: [{
title: String,
name: String,
author: String,
}]
});
I'm trying to get an output like this:
{ title: 'DQ monstes',
name: 'fooname.png',
author: 'fooauthor',
}
But I get this:
{ _id: 50f41ccff405ef73c4000006,
imagen:
[ { title: 'Pokemon',
name: 'random.png',
author: 'fooauthor',
},
{ title: 'DQ monstes',
name: 'fooname.png',
author: 'fooauthor',
}
The _id at the beginning is the req.params._id that I'm looking for at the usermodel.findOne. And I receive this error too:
/Users/rodrie/Documents/node/node_modules/mongoose/lib/utils.js:434
throw err;
^
TypeError: Cannot read property '_id' of null
Yes a find will always return the full documents. If you need to modify the returned result extracting parts of it you need to use the aggregation framework http://docs.mongodb.org/manual/applications/aggregation/. However be advised that the aggregation framework does not return a cursor so if you are iterating over more than 16MB of documents it won't help you as it will return a single document with upto 16MB of results.