【问题标题】:Mongodb : elemMatchMongodb:元素匹配
【发布时间】:2013-10-01 16:29:27
【问题描述】:

我的问题可以描述如下。

给出以下数据(从 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 },
           ]
}

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    我认为这里不能使用 $elemMatch

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

    db.schools.find( { zipcode: 63109, $and: [{"students.name": "john"}, {"students.name": "jeff"}]} )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-15
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 2016-03-26
      相关资源
      最近更新 更多