Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Java 无法从MongoDB中的文档数组中删除最低分数_Java_Mongodb - Fatal编程技术网

Java 无法从MongoDB中的文档数组中删除最低分数

Java 无法从MongoDB中的文档数组中删除最低分数,java,mongodb,Java,Mongodb,以下是我的收藏: db.students.find({_id:100}).pretty() { "_id" : 100, "name" : "Demarcus Audette", "scores" : [ { "type" : "exam", "score" : 30.61740640636871 }, { "type" : "quiz",

以下是我的收藏:

db.students.find({_id:100}).pretty()
{
    "_id" : 100,
    "name" : "Demarcus Audette",
    "scores" : [
        {
            "type" : "exam",
            "score" : 30.61740640636871
        },
        {
            "type" : "quiz",
            "score" : 14.23233821353732
        },
        {
            "type" : "homework",
            "score" : 31.41421298576332
        },
        {
            "type" : "homework",
            "score" : 30.09304792394713
        }
    ]
}
我正在Mongo shell上执行以下两个查询:

x=db.students.aggregate( [{ "$unwind":"$scores" },{"$match":{"scores.type":"homework"}},{ "$group":{"_id":"$_id","minscore":{"$min":"$scores.score" }}} ] )
下一个问题是:

for (var a in x["a"]) { db.students.update( { "_id":a["_id"] },{"$pull":{"scores":{"score":a["minscore"]}}} ) }

我可以看到min score列表,但当我执行第二个查询时,似乎没有删除任何内容。我知道有一个逻辑错误。我试着阅读这里的帖子,但我没能通过。我只是想知道我哪里出了问题。

这是因为当你这样做时:

x = db.students.aggregate([
  { "$unwind":"$scores" },
  {"$match":{
    "scores.type":"homework"
  }},
  { 
    "$group":{
       "_id":"$_id",
       "minscore":{"$min":"$scores.score" }}
    }
]);
您的结果如下表所示:

{
  result : [{
    "_id" : 100,
    "minscore" : 30.09304792394713
  }],
  ok : 1
}
因此这里没有
x[“a”]
,您需要编写
x['result']


p.S.如果你在做家庭作业问题,你应该写下来。

你的for循环中有几个问题——尽管你的聚合看起来不错

结果文档“x”包含一个数组字段“result”。您希望迭代该数组中的文档。您使用的for循环语法不会将a设置为数组中的值,而是设置为数组中的索引。更好更清晰的方法是使用forEach循环语法:

x.result.forEach(function(doc) {
   db.students.update( { "_id": doc._id }, 
                       { "$pull": { "scores" : { 
                                          "score":doc.minscore, 
                                          "type":"homework"
                       } } } 
   );
});
你的更新声明也不太准确-你想确保你只完成分数最低的家庭作业-如果同一个学生有一次考试的分数与他们的最低家庭作业的分数相同怎么办?您的语法将拉取分数等于minscore的所有子文档


这一个很容易修复(我向您展示了只拉取作业子文档所需的语法),但请记住,这与查询匹配,如果只有一个作业,那么它就是minscore,它将被拉取。如果有两个家庭作业分数相等,他们都会被拉!需要考虑的事项。

使用下面的代码删除最低分数。聚合代码没有问题

x=db.students.aggregate( [{ "$unwind":"$scores" },{"$match":{"scores.type":"homework"}},{ "$group":{"_id":"$_id","minscore":{"$min":"$scores.score" }}} ] );

var doc = myCursor.hasNext() ? x.next() : null;

x.forEach(function(doc) {db.students.update( { "_id": doc._id }, { "$pull": { "scores" : { 
"score":doc.minscore, "type":"homework"} } } );
在MongoShell运行上述代码后- db.students.find.pretty(); 您将在文档中获得如下记录-

{
    "_id" : 100,
    "name" : "Demarcus Audette",
    "scores" : [
        {
            "type" : "exam",
            "score" : 30.61740640636871
        },
        {
            "type" : "quiz",
            "score" : 14.23233821353732
        },
        {
            "type" : "homework",
            "score" : 31.41421298576332
        },
    ]
}

“家庭作业”类型的最低分数将从每个记录集中删除

这是一个与我一起工作的答案,基于其他答案的组合

x = db.students.aggregate([
  { "$unwind":"$scores" },
  {"$match":{
    "scores.type":"homework"
  }},
  { 
    "$group":{
       "_id":"$_id",
       "minscore":{"$min":"$scores.score" }}
    }
]);



x.result.forEach(function(doc) {db.students.update( { "_id": doc._id },                         { "$pull": { "scores" : {
               "score":doc.minscore,"type":"homework" } } }); });

下面的代码对我有用

var x=db.students.aggregate( [{ "$unwind":"$scores" },{"$match":{"scores.type":"homework"}},{"$group":{"_id":"$_id","minscore":{"$min":"$scores.score" }}} ] );

var doc = x.hasNext() ? x.next() : null;

x.forEach(function(doc){
    db.students.update( { "_id": doc._id }, { "$pull": { "scores" : { "score":doc.minscore, "type":"homework"} } } );
});

你试过密码了吗?这里有很多错误,仅仅将“a”更改为“result”是无法实现的。上面的代码中有一个输入错误:行:var doc=myCursor.hasNext()?x、 next():null;应读为:var doc=x.hasNext()?x、 next():null;行var doc=myCursor.hasNext()?x、 next():null;不需要删除它,代码将正常工作解决方案中存在一些打字错误和语法错误。请使用以下语句。var x=db.students.aggregate([{“$unwind”:“$scores”},{“$match”:{“scores.type”:“家庭作业”},{“$group”:{“$id”:“$\id”,“minscore”:{“$min”:“$scores.scores”}}]);var doc=x.hasNext()?x、 next():null;x、 forEach(函数(doc){db.students.update({“\u id”:doc.\u id},{“$pull”:{“分数”:{“分数”:doc.minscore,“类型”:“家庭作业”}};});这是MongoDB for java developers课程第4周的家庭作业!这个答案在MongoDB 2.4天时就已经给出了——聚合返回了一个文档,其中包含一个字段结果,该字段结果是一个数组。