findOne 返回完整文档
Posted
技术标签:
【中文标题】findOne 返回完整文档【英文标题】:findOne returning full document 【发布时间】:2021-10-03 01:33:59 【问题描述】:这是存储在MongoDB数据库中padlos集合名称下的数据
"_id" : ObjectId("60ffb89473dc672be32909c2"),
"courses" : [
"_id" : ObjectId("60ffb89473dc672be32909c3"),
"course" : "BCA",
"semesters" : [
"_id" : ObjectId("60ffb89473dc672be32909c4"),
"sem" : 1,
"subjects" : [
"_id" : ObjectId("60ffb89473dc672be32909c5"),
"subject" : "C++",
"units" : [
"_id" : ObjectId("60ffb89473dc672be32909c6"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ffb89473dc672be32909c7"),
"topic" : "Basics"
,
"_id" : ObjectId("60ffb89473dc672be32909c8"),
"topic" : "DataTypes"
]
]
,
"_id" : ObjectId("60ffb89473dc672be32909c9"),
"subject" : "IC & IT",
"units" : [
"_id" : ObjectId("60ffb89473dc672be32909ca"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ffb89473dc672be32909cb"),
"topic" : "Basics"
,
"_id" : ObjectId("60ffb89473dc672be32909cc"),
"topic" : "DataTypes"
]
]
]
,
"_id" : ObjectId("60ffb89473dc672be32909cd"),
"sem" : 2,
"subjects" : [
"_id" : ObjectId("60ffb89473dc672be32909ce"),
"subject" : "Java",
"units" : [
"_id" : ObjectId("60ffb89473dc672be32909cf"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ffb89473dc672be32909d0"),
"topic" : "Basics"
,
"_id" : ObjectId("60ffb89473dc672be32909d1"),
"topic" : "DataTypes"
]
]
,
"_id" : ObjectId("60ffb89473dc672be32909d2"),
"subject" : "SQL",
"units" : [
"_id" : ObjectId("60ffb89473dc672be32909d3"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ffb89473dc672be32909d4"),
"topic" : "Basics"
,
"_id" : ObjectId("60ffb89473dc672be32909d5"),
"topic" : "DataTypes"
]
]
]
]
],
"__v" : 0
这是我用来查询和获取数据的代码
app.get('/',function(req, res)
PadhloSchema.findOne(
"courses.course" : "BCA" , "courses.semesters.sem" : 1, "courses.semesters.subjects.subject" :"C++",
function(err, courses)
if(!err)
res.send(courses);
else
res.send(err);
)
);
我只想获取课程为 BCA、学期(sem)为 1、主题为 C++ 且响应中的数据必须如下所示的数据:
"_id" : ObjectId("60ff08a977ec48b84ec07b46"),
"subject" : "C++",
"units" : [
"_id" : ObjectId("60ff08a977ec48b84ec07b47"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ff08a977ec48b84ec07b48"),
"topic" : "Basics"
,
"_id" : ObjectId("60ff08a977ec48b84ec07b49"),
"topic" : "DataTypes"
]
]
但我是响应中的所有数据:
"_id" : ObjectId("60ffb89473dc672be32909c2"),
"courses" : [
"_id" : ObjectId("60ffb89473dc672be32909c3"),
"course" : "BCA",
"semesters" : [
"_id" : ObjectId("60ffb89473dc672be32909c4"),
"sem" : 1,
"subjects" : [
"_id" : ObjectId("60ffb89473dc672be32909c5"),
"subject" : "C++",
"units" : [
"_id" : ObjectId("60ffb89473dc672be32909c6"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ffb89473dc672be32909c7"),
"topic" : "Basics"
,
"_id" : ObjectId("60ffb89473dc672be32909c8"),
"topic" : "DataTypes"
]
]
,
"_id" : ObjectId("60ffb89473dc672be32909c9"),
"subject" : "IC & IT",
"units" : [
"_id" : ObjectId("60ffb89473dc672be32909ca"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ffb89473dc672be32909cb"),
"topic" : "Basics"
,
"_id" : ObjectId("60ffb89473dc672be32909cc"),
"topic" : "DataTypes"
]
]
]
,
"_id" : ObjectId("60ffb89473dc672be32909cd"),
"sem" : 2,
"subjects" : [
"_id" : ObjectId("60ffb89473dc672be32909ce"),
"subject" : "Java",
"units" : [
"_id" : ObjectId("60ffb89473dc672be32909cf"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ffb89473dc672be32909d0"),
"topic" : "Basics"
,
"_id" : ObjectId("60ffb89473dc672be32909d1"),
"topic" : "DataTypes"
]
]
,
"_id" : ObjectId("60ffb89473dc672be32909d2"),
"subject" : "SQL",
"units" : [
"_id" : ObjectId("60ffb89473dc672be32909d3"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ffb89473dc672be32909d4"),
"topic" : "Basics"
,
"_id" : ObjectId("60ffb89473dc672be32909d5"),
"topic" : "DataTypes"
]
]
]
]
],
"__v" : 0
请帮助并告诉我我在哪里做错了。我是 mongoDB 的新手。 我正在使用最新的 MongoDB 版本 5.0.1
Rahul Soni 评论后的更新答案
app.get('/',function(req, res)
PadhloSchema.find(
courses : course : "BCA",semesters : sem : 1 ,subjects :subject : "C++"
,
function(err, courses)
if(!err)
res.send(courses);
else
res.send(err);
)
);
但现在它给了我一个空数组
[]
【问题讨论】:
【参考方案1】:MongoDB 在文档级别返回数据。 https://docs.mongodb.com/manual/tutorial/query-array-of-documents/
基本上,它按预期工作。您正在搜索该文档,并且您得到的正是该文档。如果您正在寻找子文档,则需要像这样展开它:
db.tmp.aggregate($unwind:"$courses",$unwind:"$courses.semesters",$unwind:"$courses.semesters.subjects",$match:"courses.course":"BCA", "courses.semesters.sem": 1, "courses.semesters.subjects.subject" :"C++").pretty()
注意:我只是将您的文档添加到数据库并在 mongo 级别显示它。 Node.js 的工作方式类似。
"_id" : ObjectId("60ffd533bccc96b9985944a9"),
"courses" :
"_id" : ObjectId("60ffb89473dc672be32909c3"),
"course" : "BCA",
"semesters" :
"_id" : ObjectId("60ffb89473dc672be32909c4"),
"sem" : 1,
"subjects" :
"_id" : ObjectId("60ffb89473dc672be32909c5"),
"subject" : "C++",
"units" : [
"_id" : ObjectId("60ffb89473dc672be32909c6"),
"unit" : 1,
"topics" : [
"_id" : ObjectId("60ffb89473dc672be32909c7"),
"topic" : "Basics"
,
"_id" : ObjectId("60ffb89473dc672be32909c8"),
"topic" : "DataTypes"
]
]
【讨论】:
我已经更新了代码现在它给了我一个空数组请帮助以上是关于findOne 返回完整文档的主要内容,如果未能解决你的问题,请参考以下文章
更新 findOne()/find()/findById() 返回的文档 - mongoose