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
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.
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/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.