mongodb查询子阵列

时间:2013-08-20 00:55:02

标签: php javascript arrays mongodb

我有这个包含多个子数组的数组:

> db.alumnos111.findOne({curp:'AELJ951211MASCPN04'})
{
        "_id" : ObjectId("520d5440bee9577c283542c3"),
        "nocontrol" : "10301051680201",
        "nombre" : "JANETTE ARACELI",
        "paterno" : "ACERO",
        "materno" : "LOPEZ",
        "curp" : "AELJ951211MASCPN04",
        "generacion" : "2010-2013",
        "carrera" : "INFORMÁTICA",
        "clv_centro" : "01DCT0001F",
        "estado" : "AGUASCALIENTES",
        "plantel" : "CBTIS NO. 168",
        "id_plantel" : "513",
        "materias" : [
                {
                        "semestre" : "4",
                        "nombre_asignatra" : "ECOLOGÍA",
                        "grupo" : "4A",
                        "turno1" : "2001",
                        "parcial1" : "10",
                        "parcial2" : "9",
                        "parcial3" : "9",
                        "calificacion" : "9",
                        "periodo" : "SEMESTRAL 2 - 2011",
                        "id_periodo" : "111",
                        "id_plangpo" : "14",
                        "id_materia" : "4529"
                },
                {
                        "semestre" : "4",
                        "nombre_asignatra" : "FÍSICA I",
                        "grupo" : "4A",
                        "turno1" : "2001",
                        "parcial1" : "8",
                        "parcial2" : "10",
                        "parcial3" : "10",
                        "calificacion" : "9",
                        "periodo" : "SEMESTRAL 2 - 2011",
                        "id_periodo" : "111",
                        "id_plangpo" : "14",
                        "id_materia" : "4530"
                },          
                {
                        "semestre" : "4",
                        "nombre_asignatra" : "INGLÉS IV",
                        "grupo" : "4A",
                        "turno1" : "2001",
                        "parcial1" : "8",
                        "parcial2" : "10",
                        "parcial3" : "9",
                        "calificacion" : "9",
                        "periodo" : "SEMESTRAL 2 - 2011",
                        "id_periodo" : "111",
                        "id_plangpo" : "14",
                        "id_materia" : "4531"
                },        

        ]
}

我应该如何过滤当前{"materias.id_materia" : '$in' ("4531",'4530'),'parcial3':10}的子阵列_id

php db->find或JavaScript中的任何帮助都会有所帮助。

2 个答案:

答案 0 :(得分:2)

您的查询应该看起来像这样

db.alumnos111.find({“materias.id_materia”:{$ in:[“1234”,“5678”]}})

过滤掉文件w / id_materia =“1234”或id_materia =“5678”

此示例将直接在mongo shell中执行javascript

答案 1 :(得分:2)

你也可以试试这个:

db.alumnos111.find( { _id: id }, { materias: { $elemMatch: { id_materia: $in: [ "4531","4530" ], parcial3: 10} }