这是存储在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的noob。我使用的是最新的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条答案
按热度按时间iq0todco1#
mongodb在文档级别返回数据。https://docs.mongodb.com/manual/tutorial/query-array-of-documents/
基本上,它正在按预期工作。您正在搜索文档,并且您得到的正是该文档。如果您正在查找子文档,则需要按如下方式将其展开:
注意:我只是将您的文档添加到数据库中,并在mongo级别显示它。node.js的工作原理类似。