【发布时间】:2021-05-23 17:44:28
【问题描述】:
我有以下文件
{
"_id": ObjectId("6031dca77e510208f5d9cad5"),
"userId": ObjectId("600b49554a19f1df7e7dee6e"),
"resumes": [
{
"_id": "6031dca725f5b16515b3fde1",
"pronouns": "They/Them",
"name": "Jai",
"contact": {
"email": "jai.kirdatt@me.com",
"phone": "2035690347"
},
"location": {
"city": "Stamford",
"state": "CT"
},
"resume_name": "test",
"headline": "Full snack developer",
"summary": "Passionate full snack developer",
"links": {
"dribble": "",
"facebook": "",
"github": "",
"twitter": "",
"website": ""
},
"experience": [],
"talks": [],
"licenses_and_certifications": [],
"awards": [],
"education": [],
"side_projects": []
},
{
"_id": ObjectId("6031de2e25f5b16515b3fde2"),
"pronouns": "new",
"name": "Jai",
"contact": {
"email": "jai.kirdatt@me.com",
"phone": "2035690347"
},
"location": {
"city": "Stamford",
"state": "CT"
},
"resume_name": "new",
"headline": "new",
"summary": "new",
"links": {
"dribble": "",
"facebook": "",
"github": "",
"twitter": "",
"website": ""
},
"experience": [],
"talks": [],
"licenses_and_certifications": [],
"awards": [],
"education": [],
"side_projects": []
}
]
}
这是我从数组 resumes 中获取简历的代码
const client = dbClient();
const query = {
'resumes._id': ObjectId(resumeId)
};
await client.connect();
const collection = client.db().collection(resumeCollection);
const result = await collection.findOne(query, { 'resumes.$': 1 });
上面还是返回了两份简历。另外,我不知道在哪里放置简历。$ 只返回一份文件。
我可以通过这个命令使用 mongo shell 获取正确的文档
db.resumes.find({"resumes._id" : ObjectId("6031dca725f5b16515b3fde1")}, {'resumes.$':1})
现在我不知道如何使用 Express 的 mongoldb 驱动程序获取正确的文档。
【问题讨论】:
标签: node.js express mongodb-query