首页 > 解决方案 > 从 Express.js 查询 MongoDB 的特定字段

问题描述

我正在尝试在 MongoDB 中查询 express.js 中的某些特定字段。我尝试了几种方法,但都没有成功,包括在此处找到的建议。我确实尝试了以下示例(以及更多变体):

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

或者

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

标签: javascriptnode.jsmongodbexpress

解决方案


这是一个工作示例:

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

Ĵ


推荐阅读