Reputation: 23
I'm Trying to access one field returned from mongodb collection.find method , I can't do that and nothing is showing on the console.log
router.get('/buildings', function(req, res, next) {
var db = req.db;
var collection = db.get('buildings');
collection.find({buildingNO:"1"},{},function(e,docs){
var x=docs[0].price;
console.log(x);
});
});
Note: I'm using monk middle-ware not native mongodb
thanks
Upvotes: 0
Views: 59
Reputation: 9763
You can use The feature projection
in nodejs .
The projection is the second object you pass empty {}
so will project all attributes.
For example :
If you project an object like:
{
_id:false// or 0
}
Will omit the _id
attribute.
Here we will pass price
:
collection.find({buildingNO:"1"},{price:1},function(e,docs){
var x=docs[0].price;
console.log(x);
});
You have a typo on doc[0] should be docs
http://docs.mongodb.org/manual/reference/method/db.collection.find/
Upvotes: 0
Reputation: 22553
Check the error argument in the callback and your return argument is:
x=docs[0]...
And not:
x=doc[0]
I'm surprised you don't get an undefined variable error.
Upvotes: 1