我有以下代码。
router.get('/Child_Profile/:SchoolId/:childId',function(req,res,next){
childModel.find({"schoolid":req.params.SchoolId,"students[]":req.params.ChildId}, function (err, result) {
if (err)
{
return console.log(err);
}
res.json(result);
});
});
前集合
{
"schoolid":"1wer",
"students":["121","232"],
"profilepic":"http://wed"
}
虽然我使用schoolid和studentid找到了学生信息,但上述模式只发现了schoolid,而不是studentntid。提前致谢
答案 0 :(得分:1)
您已经提到students as an array - students[]
,只使用学生并传递studentid的值,查询将返回结果。
Mongo shell query for the collection
{
"_id" : ObjectId("59e9cfd4f4896dbabfc15f45"),
"schoolid" : "1wer",
"students" : [
"121",
"232"
],
"profilepic" : "http://wed"
}
db.collection.find({students:"121"});
修改后的查询
router.get('/Child_Profile/:SchoolId/:childId',function(req,res,next){
childModel.find({"schoolid":req.params.SchoolId,"students":req.params.ChildId}, function (err, result) {
if (err)
{
return console.log(err);
}
res.json(result);
});
});