Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB:如何从$elemMatch中仅返回/访问一个文档_Mongodb_Mongodb Query - Fatal编程技术网

MongoDB:如何从$elemMatch中仅返回/访问一个文档

MongoDB:如何从$elemMatch中仅返回/访问一个文档,mongodb,mongodb-query,Mongodb,Mongodb Query,假设我在课程集合中有一个学生对象数组字段,例如[{id:foo,name:bar,imageurl:baz}]。我只想返回在学生字段中具有给定学生id的课程 如果我使用$elemMatch,我将在课程集合中获得一堆文档以及我想要的课程。有没有办法解决这个问题,或者我一定要做一些查询后过滤来只返回一个文档 编辑: 示例课程集: { _id: 1, course: "OOP 101", students: [ { name: "ajax", school: 100,

假设我在课程集合中有一个学生对象数组字段,例如
[{id:foo,name:bar,imageurl:baz}]
。我只想返回在学生字段中具有给定学生id的课程

如果我使用
$elemMatch
,我将在课程集合中获得一堆文档以及我想要的课程。有没有办法解决这个问题,或者我一定要做一些查询后过滤来只返回一个文档

编辑: 示例课程集:

{
 _id: 1,
 course: "OOP 101",
 students: [
              { name: "ajax", school: 100, age: 7 },
              { name: "achilles", school: 100, age: 8 },
           ]
}
{
 _id: 2,
 course: "Programming 101",
 students: [
              { name: "john", school: 102, age: 10 },
              { name: "ajax", school: 100, age: 7 },
              { name: "achilles", school: 100, age: 8 },
           ]
}
{
 _id: 3,
 course: "Database 101",
 students: [
              { name: "john", school: 102, age: 10 },
              { name: "jess", school: 102, age: 11 },
              { name: "jeff", school: 108, age: 15 }
           ]
}
如果我使用
$elemMatch
查找jeff正在学习的课程,则预期输出:

{
 _id: 3,
 course: "Database 101",
 students: [
              { name: "john", school: 102, age: 10 },
              { name: "jess", school: 102, age: 11 },
              { name: "jeff", school: 108, age: 15 }
           ]
}

{
 _id: 1,
 course: "OOP 101"
{
 _id: 2,
 course: "Programming 101"
} 
{
 _id: 3,
 course: "Database 101",
 students: [
              { name: "john", school: 102, age: 10 },
              { name: "jess", school: 102, age: 11 },
              { name: "jeff", school: 108, age: 15 }
           ]
}
我只想返回在学生字段中具有给定学生id的课程

可以使用根据嵌入文档中字段的值选择文档。然后使用一个简单的投影,您只能返回感兴趣的字段:

> db.test.courses.find(
     {students: {$elemMatch : { name: "john"}}},  // find all courses having 
                                                  // "john" as a student
     {course:1})                                  // keep only the course name
                                                  // and the _id
{ "_id" : 2, "course" : "Programming 101" }
{ "_id" : 3, "course" : "Database 101" }

当然,如果需要整个文档,请不要指定任何投影:

> db.test.courses.find(      {students: {$elemMatch : { name: "jeff"}}}).pretty()
{
    "_id" : 3,
    "course" : "Database 101",
    "students" : [
        {
            "name" : "john",
            "school" : 102,
            "age" : 10
        },
        {
            "name" : "jess",
            "school" : 102,
            "age" : 11
        },
        {
            "name" : "jeff",
            "school" : 108,
            "age" : 15
        }
    ]
}
请注意,这将返回所有课程,其中有一名学生具有匹配的姓名。在我们的样本数据中,“jeff”仅与一门课程相关。但这将返回“john”的两个文档

如果您只需要其中一个,您可以将聚合框架与
$limit
阶段一起使用(对于大多数情况,您应该在
$limit
之前使用
$sort
阶段):


你能至少向我们展示一些样本文档和预期的输出吗?我已经编辑了你的问题,根据你给出的一个链接展示了一个样本集合。你能举一个预期输出的例子吗?@SylvainLeroux谢谢你的帮助。我已经编辑了问题以显示预期的输出!嗨,我把问题编辑得更具体了。mongodb文档似乎显示了一个包含不相关课程的响应(其中students字段不匹配,返回课程,但隐藏students字段)。“我错了吗?”wemadeit谢谢你澄清了你的需求。AFAIKT,使用我解释过的
$elemMatch
,它应该会产生您所期望的结果。我已经编辑以提供更多细节。使用MongoDB 3.0.2进行测试。MongoDB文档说不会发生这种情况。查看zipcode搜索示例:其中{“_id”:3}作为结果返回,即使它不是一个match@wemadeit
$elemMatch
可以在两种不同的上下文中使用。a(这是您链接的文档中描述的用例)和a(这是我在这里使用的)。请花些时间比较这两个文档并进行实验。我向您保证,在发布答案之前,我运行了这些命令,它是有效的。。。如查询运算符所述。
> db.test.courses.aggregate([
        {$match:{students: {$elemMatch : { name: "john"}}}},
        {$limit:1}
  ]).pretty()
{
    "_id" : 2,
    "course" : "Programming 101",
    "students" : [
        {
            "name" : "john",
            "school" : 102,
            "age" : 10
        },
        {
            "name" : "ajax",
            "school" : 100,
            "age" : 7
        },
        {
            "name" : "achilles",
            "school" : 100,
            "age" : 8
        }
    ]
}