How to delete the documents returned by group in mongodb?
db.grades.aggregate( [
{
$match:{type:'homework'}
},
{ $group:
{ _id: {student_id:"$student_id",type:'$type'},
score: { $max: "$score" }
}
}
]).forEach(function(doc){
db.grades.remove({'student_id':doc._id.student_id,'score':doc.score})
})
Use the cursor result from the aggregation to loop through the documents with the cursor's forEach()
method and then remove each document from the collection using the _id
as the query in the remove()
method. Something like this:
var cursor = db.grades.aggregate(pipeline);
cursor.forEach(function (doc){
db.grades.remove({"_id": doc._id});
});
Another approach is to create an array of the document's _id
s using the map()
method and remove the documents like:
var cursor = db.grades.aggregate(pipeline),
ids = cursor.map(function (doc) { return doc._id; });
db.grades.remove({"_id": { "$in": ids }});
-- UPDATE --
For large deletion operations, it may be more efficient to copy the documents that you want to keep to a new collection and then use drop()
on the original collection. To copy the essential documents your aggregation pipeline needs to return the documents without the lowest homework doc and copy them to another collection using the $out
operator as the final pipeline stage. Consider the following aggregation pipeline:
db.grades.aggregate([
{
'$group':{
'_id': {
"student_id": "$student_id",
"type": "$type"
},
'lowest_score': { "$min": '$score'},
'data': {
'$push': '$$ROOT'
}
}
},
{
"$unwind": "$data"
},
{
"$project": {
"_id": "$data._id",
"student_id" : "$data.student_id",
"type" : "$data.type",
"score" : "$data.score",
'lowest_score': 1,
"isHomeworkLowest": {
"$cond": [
{
"$and": [
{ "$eq": [ "$_id.type", "homework" ] },
{ "$eq": [ "$data.score", "$lowest_score" ] }
]
},
true,
false
]
}
}
},
{
"$match": {"isHomeworkLowest" : false}
},
{
"$project": {
"student_id": 1,
"type": 1,
"score": 1
}
},
{
"$out": "new_grades"
}
])
in which you can then drop the old collection by db.grades.drop()
and then query on db.new_grades.find()
I think this is the a database part of homework of MongoDB for Java Developers provided by MongoDB University. Where the requirement is to delete the lowest score from each individual student. anyway I solved this way. I hope It will be helpful for you. You can also clone my code from my github link(Provided below)
public class Homework2Week2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Here the the documentation is used for mongo-jva-driver-3.2.2.jar
/*If you want to use different versionof mongo-jva-driver
then you have look for that version specificatios.*/
MongoClient mongoClient = new MongoClient();
// get handle to "students" database
MongoDatabase database = mongoClient.getDatabase("students");
// get a handle to the "grades" collection
MongoCollection<Document> collection = database.getCollection("grades");
/*
* Write a program in the language of your choice that will remove the grade of type "homework" with the lowest score for each student from the dataset in the handout.
* Since each document is one grade, it should remove one document per student.
* This will use the same data set as the last problem, but if you don't have it, you can download and re-import.
* The dataset contains 4 scores each for 200 students.
* First, letâs confirm your data is intact; the number of documents should be 800.
*Hint/spoiler: If you select homework grade-documents, sort by student
and then by score, you can iterate through and find the lowest score
for each student by noticing a change in student id. As you notice
that change of student_id, remove the document.
*/
MongoCursor<Document> cursor = collection.find(eq("type", "homework")).sort(new Document("student_id", 1).append("score", 1)).iterator();
int curStudentId = -1;
try
{
while (cursor.hasNext()) {
Document doc = cursor.next();
int studentId=(int) doc.get("student_id");
if (studentId != curStudentId) {
collection.deleteMany(doc);
curStudentId = studentId;
}
}
}finally {
//Close cursor
cursor.close();
}
//Close mongoClient
mongoClient.close();
}
}
In my Github account I have the complete project code. If anyone want's you can try from this link.