0

I am trying to query a MongoDB for some specific fields from express.js. I have tried several approaches, but without success, including the suggestions found here. I have indeed tried as examples the following (and a few more variations):

 db.collection("Items").find({}, { Name : 1, Price : 1 }, (err, result) => {
  if (result) {
    console.log(result)
  }
 })

Or

 db.collection("Items").aggregate( { $project : { Name : 1, Price : 1 } }, (err, result) => {
  if (result) {
   console.log(result)
  }
})
3
  • 1
    Are you using mongoose or the official node.js driver? Commented Nov 3, 2020 at 22:43
  • Thank you Montgomery Watts. I am using the node.js driver. Commented Nov 4, 2020 at 2:27
  • Does this answer your question? Mongo Call does not remove parameters Commented Nov 4, 2020 at 2:40

1 Answer 1

0

Here is a working example:

 db.collection("Items").find({}, { projection: { Name : 1, Price : 1 } }).toArray( (err, result) => {
   if (result) {
   console.log(result)
   }
 })

J

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.