Z
Z
zimy012016-10-26 02:13:38
MongoDB
zimy01, 2016-10-26 02:13:38

How to remove a field in MongoDB from the command line?

Hello, the task ahead is:
There are 800 fields with marks

> 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

Of these, 400 fields contain data on grades with homework
> db.grades.find({type:'homework'}).count()
400

It is necessary to select all fields with homework and sort them by students_id and 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

Then you need to completely erase the fields with records of minimum homework grades for each student.
That is, in the end, we should get the result of such a plan:
> 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

Can you please tell me how to delete the line with the minimum mark on the remote sensing ... Or did I make the request wrong (even though it displays the correct values ​​on the screen), that I can’t shove it into .deleteMany() , .updateMany()?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Philipp, 2016-10-26
@zoonman

Read https://docs.mongodb.com/v3.2/reference/method/db....
Read about $unset
Read about multi: true

Z
zimy01, 2016-10-26
@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

What am I doing wrong?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question