项目数组值
学生收藏包含以下文件:
{ "_id" : 1, "semester" : 1, "grades" : [ 70, 87, 90 ] }
{ "_id" : 2, "semester" : 1, "grades" : [ 90, 88, 92 ] }
{ "_id" : 3, "semester" : 1, "grades" : [ 85, 100, 90 ] }
{ "_id" : 4, "semester" : 2, "grades" : [ 79, 85, 80 ] }
{ "_id" : 5, "semester" : 2, "grades" : [ 88, 88, 92 ] }
{ "_id" : 6, "semester" : 2, "grades" : [ 95, 90, 96 ] }
在以下查询中,投影{“grade。$”:1}仅返回年级大于或等于85的第一个元素。
db.students.find( { semester: 1, grades: { $gte: 85 } }, { "grades.$": 1 } )
该操作返回以下文档:
{ "_id" : 1, "grades" : [ 87 ] }
{ "_id" : 2, "grades" : [ 90 ] }
{ "_id" : 3, "grades" : [ 85 ] }
但在以下结果中我也需要“学期”值。并且如果它在与学期相同的位置有多个值。
编辑:同一位置的多个值,如果集合如下:
{ "_id" : 1, "semester" : 1, "subject": "Maths", "class": "5", "division" : "A" "grades" : [ 70, 87, 90 ] }
{ "_id" : 2, "semester" : 1, "subject": "Maths", "class": "5", "division" : "A" "grades" : [ 90, 88, 92 ] }
{ "_id" : 3, "semester" : 1, "subject": "Maths", "class": "5", "division" : "A" "grades" : [ 85, 100, 90 ] }
{ "_id" : 4, "semester" : 2, "subject": "Maths", "class": "5", "division" : "A" "grades" : [ 79, 85, 80 ] }
{ "_id" : 5, "semester" : 2, "subject": "Maths", "class": "5", "division" : "A" "grades" : [ 88, 88, 92 ] }
{ "_id" : 6, "semester" : 2, "subject": "Maths", "class": "5", "division" : "A" "grades" : [ 95, 90, 96 ] }
即。集合包含多个字段,如“主题”,“类”,“分区”在“学期”中的相同位置