2013-08-20 168 views
1

我有這種陣列與多個子陣列: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" 
       },   

     ] 
} 

我應該如何篩選子陣列(一個或多個)當前_id{"materias.id_materia" : '$in' ("4531",'4530'),'parcial3':10}

任何幫助php db->find或JavaScript將有所幫助。

回答

2

你的查詢應該看起來像這樣

db.alumnos111.find({ 「materias.id_materia」:在{$: 「1234」, 「5678」]}})

過濾出的文檔W/id_materia = 「1234」 或id_materia = 「5678」

直在蒙戈外殼這個例子將JavaScript的執行

2

你也可以試試這個:

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

謝謝你所有的答案,最後一個做到了這個訣竅,雖然mongo <2.2中的$ elemMatch並不能很好地工作。我需要升級到2.4.5版本 –