javascript - 从 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)
}
})
解决方案
这是一个工作示例:
db.collection("Items").find({}, { projection: { Name : 1, Price : 1 } }).toArray( (err, result) => {
if (result) {
console.log(result)
}
})
Ĵ
推荐阅读
- php - 如何在php中收集关于特定日期值的日期?
- spring-boot - Docker容器中的Java Spring Boot缓存生成失败
- visual-studio - 查询频道时出错:身份“管理员”未注册或未加载
- javascript - 从 react-admin 中的自定义函数回调修改特定的 redux-form 值
- java - 如何删除 spring-data-jpa 中位于多个侧面的项目?
- android - RecyclerView 滚动缓慢
- java - 为 Spring Service 实现 JUnit 测试
- python - 如何从数据框中创建项目字典?
- android - 为什么在 ConstraintLayout 中一项不在另一项之上
- php - 使用合并操作迭代数组并通过引用更改数组项时的错误行为