Mongo 投影查询结果与 shell 与 nodejs 应用程序中的测试不同 [英] Mongo projection query results vary from testing in shell vs nodejs application
本文介绍了Mongo 投影查询结果与 shell 与 nodejs 应用程序中的测试不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从查询的文档中过滤字段
Hi I'm trying to filter fields from queried doc
这里是样本数据
db.inventory.insertMany( [
{_id: 1, item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{_id: 2, item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{_id: 3, item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{_id: 4, item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{_id: 5, item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
查询db.inventory.find({_id:1},{size:0}).pretty()
在抑制尺寸字段的文档下方返回
returns below doc suppressing the size feild
{ "_id" : 1, "item" : "journal", "qty" : 25, "status" : "A" }
在 node js 应用中实现相同的
implementing the same in node js application
db.collection("inventory").findOne({ _id: 1 }, { "size": 0 }, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
})
按原样返回文档而不抑制大小字段.
returns the doc as is without suppressing the size field.
{
"_id": 1,
"item": "journal",
"qty": 25,
"size": {
"h": 14,
"w": 21,
"uom": "cm"
},
"status": "A"
}
如果我在这里遗漏了什么或做错了什么,请告诉我.我不想使用猫鼬.
Please let me know if I'm missing something here or doing it wrong. I don't want to use Mongoose.
推荐答案
findOne 函数在 node.js 驱动程序中的定义与 shell 中的函数略有不同.
The findOne function in the node.js driver has a slightly different definition than the one in the shell.
试试
db.collection("inventory").findOne({ _id: 1 }, {projection:{ "size": 0 }}, (err, result) => {
这篇关于Mongo 投影查询结果与 shell 与 nodejs 应用程序中的测试不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文