Pika
Pika

Reputation: 154

nodejs/mongodb - reading out one specific element

I want to read out one specific element out of mongodb

db.collection('profiles', function(err, collection) {
    collection.findOne({'email': mail}, function(err, item) {

this reads the whole entry
for example:

{
  "email" : "[email protected]",
  "password" : "asd",
  "_id" : ObjectId("51c8790f912501e403000001")
}

how can i read out only one of those elements
for example password

{
  "password" : "asd"
}

Upvotes: 0

Views: 63

Answers (1)

c.P.u1
c.P.u1

Reputation: 17094

collection.findOne({'email': mail}, {password: 1, _id: 0}, function(err, item) {
}

The second argument to find/findOne is the fields to select(projection). {_id: 0} is explicitly required because by default _ids are always returned.

Upvotes: 2

Related Questions