Istabraq Mahmood
Istabraq Mahmood

Reputation: 29

How to retrieve raw values from mongoDB using Node.js?

I'm using mongoDB and Node.js and my JSON like :

[ [ { _id: 5874e9cf97a7f81db1fff5ca,
  createdAt: Tue Jan 10 2017 06:03:59 GMT-0800 (PST),
  pulses: 75 },
{ _id: 5874ea0173a3071dbd4ed5df,
  createdAt: Tue Jan 10 2017 06:04:49 GMT-0800 (PST),
  pulses: 75 },
{ _id: 5874eb138252e01de9737e07,
  createdAt: Tue Jan 10 2017 06:09:23 GMT-0800 (PST),
  pulses: 75 },
{ _id: 5875f89d375be10b42c08749,
  createdAt: Wed Jan 11 2017 01:19:25 GMT-0800 (PST),
  pulses: 75 },
{ _id: 5875f8d49f08d00b4e9d2117,
  createdAt: Wed Jan 11 2017 01:20:20 GMT-0800 (PST),
  pulses: 75 },
{ _id: 5876079811de080d2c7e59f1,
  createdAt: Wed Jan 11 2017 02:23:20 GMT-0800 (PST),
  pulses: 75 } ] ]

I want to retrieve only the values 75 in filed pulses. In mongo site, I see find() and findOne() but they do not show what I want.

Upvotes: 0

Views: 215

Answers (1)

Supradeep
Supradeep

Reputation: 3266

Use find() to get all the docuemnts with {pulses: 75} like :

db.collection.find({pulses: 75});

Or, Use findOne() to get all the documents with {pulses: 75} like :

db.collection.findOne({pulses: 75});

If you just want to see only the pulses field in the results , add projection as shown below :

db.collection.find({pulses: 75}, {pulses:1})

Assuming your outer array key is pulsesArray, for nested arrays, you can do like this:

db.collection.find({pulsesArray.0.pulses: 75});

Upvotes: 2

Related Questions