Read from mongodb DBCursor - Java Big Data

Java examples for Big Data:MongoDB

Description

Read from mongodb DBCursor

Demo Code


import com.mongodb.BasicDBObject;
import com.mongodb.BulkWriteOperation;
import com.mongodb.Cursor;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import java.util.List;
import java.util.Set;
import javax.swing.JOptionPane;
import java.sql.*;

public class JoinQuery extends javax.swing.JFrame {

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        DBCursor cursor = null;/*from   w w  w. j a  v  a  2 s.  c  o m*/
        MongoClient mongoClient = null;
        try {
            mongoClient = new MongoClient("localhost", 27017); //initialize connection
            DB db = mongoClient.getDB("mydb"); //connect to database, if no authentication then this will work
            DBCollection coll = db.getCollection("posts");//from database object, get collection with date
            BasicDBObject query = new BasicDBObject();
            long timeNow = System.nanoTime();
            cursor = coll.find();
            long timeAfter = System.nanoTime() - timeNow;
            while (cursor.hasNext()) {
                DBObject o = cursor.next();
                System.out.println(o);
            }
            jTextField2.setText("" + timeAfter);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        } finally {
            cursor.close();//cleanup
            mongoClient.close();
        } // TODO add your handling code here:
    }//GEN-LAST:event_jButton6ActionPerformed

}

Related Tutorials