@zimy01

Как удалить поле в MongoDB из командной строки?

Здравствуйте, предстоит задача:
Имеется 800 полей с отметками
> db.grades.find().count()
800
> db.grades.find()
{ "_id" : ObjectId("50906d7fa3c412bb040eb57a"), "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb579"), "student_id" : 0, "type" : "homework", "score" : 14.8504576811645 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb578"), "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb577"), "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57b"), "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57e"), "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57d"), "student_id" : 1, "type" : "homework", "score" : 21.33260810416115 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57f"), "student_id" : 2, "type" : "exam", "score" : 19.88180838833524 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb582"), "student_id" : 2, "type" : "homework", "score" : 97.75889721343528 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb581"), "student_id" : 2, "type" : "homework", "score" : 60.9750047106029 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb584"), "student_id" : 3, "type" : "quiz", "score" : 82.59760859306996 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb583"), "student_id" : 3, "type" : "exam", "score" : 92.6244233936537 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb585"), "student_id" : 3, "type" : "homework", "score" : 50.81577033538815 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb586"), "student_id" : 3, "type" : "homework", "score" : 92.71871597581605 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb580"), "student_id" : 2, "type" : "quiz", "score" : 1.528220212203968 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb58a"), "student_id" : 4, "type" : "homework", "score" : 28.656451042441 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb58b"), "student_id" : 5, "type" : "exam", "score" : 49.41260067227861 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb587"), "student_id" : 4, "type" : "exam", "score" : 87.89071881934647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb588"), "student_id" : 4, "type" : "quiz", "score" : 27.29006335059361 }
Type "it" for more

Из них 400 полей содержат данные об отметках с домашним заданием
> db.grades.find({type:'homework'}).count()
400

Необходимо выбрать все поля с домашним заданием и отсортировать их по students_id и score
> db.grades.aggregate([{'$match':{type:'homework'}}, {'$group':{'_id':'$student_id', 'score':{$min:'$score'}}}, {'$sort':{_id:1}}])
{ "_id" : 0, "score" : 14.8504576811645 }
{ "_id" : 1, "score" : 21.33260810416115 }
{ "_id" : 2, "score" : 60.9750047106029 }
{ "_id" : 3, "score" : 50.81577033538815 }
{ "_id" : 4, "score" : 5.244452510818443 }
{ "_id" : 5, "score" : 23.29430953857654 }
{ "_id" : 6, "score" : 81.23822046161325 }
{ "_id" : 7, "score" : 63.35102050393443 }
{ "_id" : 8, "score" : 66.42784200049636 }
{ "_id" : 9, "score" : 16.60130789148128 }
{ "_id" : 10, "score" : 6.094174990746648 }
{ "_id" : 11, "score" : 27.42742674795513 }
{ "_id" : 12, "score" : 51.69676516705788 }
{ "_id" : 13, "score" : 0.4838914493376478 }
{ "_id" : 14, "score" : 63.69676709320795 }
{ "_id" : 15, "score" : 7.475648374118382 }
{ "_id" : 16, "score" : 12.8489522283682 }
{ "_id" : 17, "score" : 32.33461505658801 }
{ "_id" : 18, "score" : 16.31570053358192 }
{ "_id" : 19, "score" : 19.50958720409689 }
Type "it" for more

Затем нужно полностью стереть поля с записями минимальных оценок по домашнему заданию для каждого студента.
То есть в конечном итоге мы должны получить результат такого плана:
> db.grades.find()
{ "_id" : ObjectId("50906d7fa3c412bb040eb57a"), "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb578"), "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb577"), "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57b"), "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57e"), "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57f"), "student_id" : 2, "type" : "exam", "score" : 19.88180838833524 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb582"), "student_id" : 2, "type" : "homework", "score" : 97.75889721343528 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb584"), "student_id" : 3, "type" : "quiz", "score" : 82.59760859306996 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb583"), "student_id" : 3, "type" : "exam", "score" : 92.6244233936537 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb586"), "student_id" : 3, "type" : "homework", "score" : 92.71871597581605 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb580"), "student_id" : 2, "type" : "quiz", "score" : 1.528220212203968 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb58b"), "student_id" : 5, "type" : "exam", "score" : 49.41260067227861 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb587"), "student_id" : 4, "type" : "exam", "score" : 87.89071881934647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb588"), "student_id" : 4, "type" : "quiz", "score" : 27.29006335059361 }
Type "it" for more

Подскажите пожалуйста, как удалить строку с минимальной отметкой по ДЗ... Или я не так запрос составил(хоть на экран выводит верные значения), что не получается его впихнуть в .deleteMany() , .updateMany()?
  • Вопрос задан
  • 613 просмотров
Пригласить эксперта
Ответы на вопрос 2
zoonman
@zoonman
⋆⋆⋆⋆⋆
Читать https://docs.mongodb.com/v3.2/reference/method/db....

Читать про $unset

Читать про multi: true
Ответ написан
@zimy01 Автор вопроса
> db.grades.update({{'$match':{type:'homework'}}, {'$group':{'_id':'$student_id', 'score':{$min:'$score'}}}}, {$unset: {score:1}}, {multi: true})
2016-10-26T12:52:04.706+0300 E QUERY    [thread1] SyntaxError: invalid property id @(shell):1:18

Что я делаю не так?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы