com.chadcover.Homework_23.java Source code

Java tutorial

Introduction

Here is the source code for com.chadcover.Homework_23.java

Source

/*
 * Copyright 2015 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.chadcover;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import java.util.ArrayList;
import java.util.List;

import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Filters.gte;
import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
import static com.mongodb.m101j.util.Helpers.printJson;

import static com.mongodb.client.model.Sorts.ascending;
import static com.mongodb.client.model.Sorts.descending;
import static com.mongodb.client.model.Sorts.orderBy;
import static com.mongodb.m101j.util.Helpers.printJson;

public class Homework_23 {
    public Homework_23() {
    }

    public static void main(String[] args) {
        MongoClient client = new MongoClient();
        MongoDatabase database = client.getDatabase("students");
        MongoCollection<Document> collection = database.getCollection("gradesBak");

        long count = collection.count();
        System.out.println(count);

        Bson filter = eq("type", "homework");
        Bson sort = ascending("student_id", "score");
        Bson projection = fields(include("student_id", "score"));

        // better for large datasets
        MongoCursor<Document> result = collection.find(filter).sort(sort).projection(projection).iterator();
        try {
            int lastId = 0;
            int counter = 0;
            while (result.hasNext()) {
                Document cur = result.next();
                int id = (Integer) cur.get("student_id");
                // when moving to new student
                // delete that record, which is the lowest homework score
                if (id != lastId || counter == 0) {
                    double grade = (Double) cur.get("score");
                    System.out.println("Delete this score: " + grade);
                    collection.deleteOne(eq("_id", cur.get("_id")));
                } else {
                    // do nothing
                }
                lastId = id;
                counter++;
            }
        } finally {
            result.close();
        }

    }
}