com.mycompany.database.MongodbDataBaseClass.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.database.MongodbDataBaseClass.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.database;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

import java.util.ArrayList;
import java.util.List;
import org.bson.Document;

/**
 *
 * @author beethoven
 */
public class MongodbDataBaseClass {

    private MongoClient mongoClient;
    private DB db;
    private boolean authenticate;
    private DBCollection table;

    public MongodbDataBaseClass() {
    }

    public void dududu() {
        System.out.println("not error");
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase database = mongoClient.getDatabase("testdb");
        System.out.println("not error");
        MongoCollection<Document> col = database.getCollection("cars");

        try (MongoCursor<Document> cur = col.find().iterator()) {
            while (cur.hasNext()) {

                Document doc = cur.next();

                List list = new ArrayList(doc.values());
                System.out.print(list.get(1));
                System.out.print(": ");
                System.out.println(list.get(2));
            }
        }

        mongoClient.close();
    }

}