0

我的问题可以描述如下。

给出以下数据(从 mongo 手册中复制),我怎样才能找到邮政编码为 63109 并且有 2 个名为“John”和“Jeff”的学生的文档。我试试

db.schools.find({ 邮政编码:63109 },{学生:{ $elemMatch:{姓名:{“约翰”,“杰夫”}}}})

但它不起作用。请问你能帮帮我吗 ?谢谢先进

{
 _id: 1,
 zipcode: 63109,
 students: [
              { name: "john", school: 102, age: 10 },
              { name: "jess", school: 102, age: 11 },
              { name: "jeff", school: 108, age: 15 }
           ]
}
{
 _id: 2,
 zipcode: 63110,
 students: [
              { name: "ajax", school: 100, age: 7 },
              { name: "achilles", school: 100, age: 8 },
           ]
}

{
 _id: 3,
 zipcode: 63109,
 students: [
              { name: "ajax", school: 100, age: 7 },
              { name: "achilles", school: 100, age: 8 },
           ]
}

{
 _id: 4,
 zipcode: 63109,
 students: [
              { name: "barney", school: 102, age: 7 },
           ]
}
4

1 回答 1

2

我认为这里不可能使用 $elemMatch

我看到的唯一解决方案是:

db.schools.find( { zipcode: 63109, $and: [{"students.name": "john"}, {"students.name": "jeff"}]} )
于 2013-09-25T22:12:43.583 回答