PRUDHVI RAJ
PRUDHVI RAJ

Reputation: 41

how to get values in mongodb using find

I have this data in my database.

"_id" : ObjectId("5c1b34d11aa970061a76aa68"),
        "city" : "Delhi"

I tried to get city like this.

 db.myCollection.find({_id:ObjectId("5c1b34d11a970061a76aa68")},{_id:0,city:1})

I am getting output { "city" : "Delhi" } like this. but i need only Delhi without quotes and "city". Is it possible.

Upvotes: 0

Views: 58

Answers (2)

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

Use toArray() and map()

db.myCollection
    .find({_id:ObjectId("5c1b34d11a970061a76aa68")},{ _id:0, city:1 })
    .toArray()
    .map(function(result){ return result.city;})

Upvotes: 1

Kuldeep Mishra
Kuldeep Mishra

Reputation: 4040

db.myCollection.find({_id:ObjectId("5c1b34d11a970061a76aa68")},function(err,result){
 res.render('yourviewpath', {
        response: result
    });

});
in view you can access like tbis as i am using ejs <%=response.result.city=>

it will print only city

Upvotes: 0

Related Questions