如何使用 Node.js 仅获取 MongoDB 中的部分文档?
How to get only part of document in MongoDB with Node.js?
"Geography": {
"Tourism": {
"B001": {
"code": "B001",
"max": " 140",
"min": " 97",
"minWithQuota": " 91"
}
}
},
"MathAndPhysic": {
"IT": {
"B183": {
"code": "B183",
"max": "140",
"min": " 50",
"minWithQuota": "none",
"quotes": []
}
}
}
我有一些 collection,我只想买“B001”object。我试过了
db.subjects.findOne (
{"Geography.Tourism.B001": {$exists: true}},
{_id: 1," Geography.Tourism.B001": 1})
但结果是整个文档。我如何得到这个结果?
"B001": {
"code": "B001",
"max": " 140",
"min": " 97",
"minWithQuota": " 91"
}
P.S。有趣的是,如果您尝试在 MongoShell 中使用相同的命令,您将获得所需的结果
尝试使用参考 $
、
投影场
db.subjects.findOne(
{ "Geography.Tourism.B001": { $exists: true } },
{ "B001": "$Geography.Tourism.B001" }
);
"Geography": {
"Tourism": {
"B001": {
"code": "B001",
"max": " 140",
"min": " 97",
"minWithQuota": " 91"
}
}
},
"MathAndPhysic": {
"IT": {
"B183": {
"code": "B183",
"max": "140",
"min": " 50",
"minWithQuota": "none",
"quotes": []
}
}
}
我有一些 collection,我只想买“B001”object。我试过了
db.subjects.findOne (
{"Geography.Tourism.B001": {$exists: true}},
{_id: 1," Geography.Tourism.B001": 1})
但结果是整个文档。我如何得到这个结果?
"B001": {
"code": "B001",
"max": " 140",
"min": " 97",
"minWithQuota": " 91"
}
P.S。有趣的是,如果您尝试在 MongoShell 中使用相同的命令,您将获得所需的结果
尝试使用参考 $
、
db.subjects.findOne(
{ "Geography.Tourism.B001": { $exists: true } },
{ "B001": "$Geography.Tourism.B001" }
);