Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在mongoDB中过滤子文档?_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript 如何在mongoDB中过滤子文档?

Javascript 如何在mongoDB中过滤子文档?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我有这份mlab文件: { "_id": { "$oid": "572b2cdfc80eb653c302f5e9" }, "year": 2014, "students": [ { "id": 5, "firstName": "Joe", "lastName": "know", "GPA": 67 }, { "id": 3, "firstName": "Peter",

我有这份mlab文件:

{
"_id": {
    "$oid": "572b2cdfc80eb653c302f5e9"
},
"year": 2014,
"students": [
    {
        "id": 5,
        "firstName": "Joe",
        "lastName": "know",
        "GPA": 67
    },
    {
        "id": 3,
        "firstName": "Peter",
        "lastName": "Jones",
        "GPA": 77
    },
    {
        "id": 6,
        "firstName": "Yossi",
        "lastName": "Haim",
        "GPA": 68
    },
    {
        "id": 13,
        "firstName": "Chen",
        "lastName": "Si",
        "GPA": 92
    }
]
}
我得到:(年份也是一个参数)


除了从集合中获取数据外,我做得很好,但我希望从返回的文档中排除所有学生数组记录,在这些记录中,GPA可以$unwind students,对其进行筛选,然后添加到组中-请参见下面的示例:

db.yush.aggregate([
{$match:{"year":2014}},
{
            $unwind : "$students"
        }, {
            $match : {
                "students.GPA" : {
                    $lt : 90
                }                
            }
        }, {
            $group : {
                _id : "$year"
                ,
                students : {
                    $push : "$students"
                }
            }
        }
    ])
最后使用$project将_id更改为年


玩得开心-欢迎任何评论

您可以尝试使用聚合

gradeM.aggregate( {$match: {year: 2014}},{ $unwind: '$students'},
                   { $match: {'students.GPA': {$lt: 90}}},
                   { $group: {_id: '$_id',
                              year : '$year',
                              students: {$push: {id:'$students.id',
                                                 firstName:'$students.firstName'}}}})
输出:

{ "_id" : ObjectId("572b2cdfc80eb653c302f5e9"), 
  "year":2014,
   "students" : [ { "id" : 5, "firstName" : "Joe" },
                  { "id" : 3, "firstName" : "Peter" },
                  { "id" : 6, "firstName" : "Yossi" } ] }
还有,你可以试试看

gradeM.aggregate([
    { $match: {year: year}},
    { $project: {
        students: {$filter: {
            input: '$students',
            as: 'students',
            cond: {$lt: ['$$students.GPA', 90]}
        }}
        }}
    ])
你可以用

尝试如下

gradeM= mongoose.model('Grade',grade);
  gradeM.find({'year':year, students: { $elemMatch: GPA: { $lt: 90 } }},'-_id').exec(function (err, data) {
      if (err) console.log("err: " + err);
  console.log(JSON.stringify(data)); 
  });

这看起来不错,它给了我所有需要的GPA>=90,但它返回它所有的年份。如何根据进入函数的年份参数进行过滤?谢谢lot@YushN添加$match阶段作为第一个只处理所选年份的阶段,对于explixit year非常有效,但是一旦我写入参数year(具有相同的值),我就会得到[]。Thanks@YushN-嗯,这是strane-没有主意,获胜者是:“year”:parseInt(year)@profesor79下一个在列表中,试图了解如何使用聚合回调返回作为http响应,我可以在浏览器上显示。根据我掌握的数据,第一个返回[]不应该为空。若我并没有弄错的话,第二个会下降,因为mongoDB版本不识别$filter。谢谢你的帮助,让我试试。。这是一个很好的$filter方法-喜欢!非常感谢。很乐意帮忙
gradeM.aggregate([
    { $match: {year: year}},
    { $project: {
        students: {$filter: {
            input: '$students',
            as: 'students',
            cond: {$lt: ['$$students.GPA', 90]}
        }}
        }}
    ])
gradeM= mongoose.model('Grade',grade);
  gradeM.find({'year':year, students: { $elemMatch: GPA: { $lt: 90 } }},'-_id').exec(function (err, data) {
      if (err) console.log("err: " + err);
  console.log(JSON.stringify(data)); 
  });