Example usage for com.mongodb.client MongoCursor hasNext

List of usage examples for com.mongodb.client MongoCursor hasNext

Introduction

In this page you can find the example usage for com.mongodb.client MongoCursor hasNext.

Prototype

@Override
    boolean hasNext();

Source Link

Usage

From source file:com.imos.sample.SampleMongoDB.java

public static void main(String[] args) {
    MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
    MongoClient mongoClient = new MongoClient(connectionString);
    MongoDatabase database = mongoClient.getDatabase("sampledb");
    MongoCollection<Document> collection = database.getCollection("sampleLog");
    System.out.println(collection.count());
    MongoCursor<Document> cursor = collection.find().iterator();
    ObjectMapper mapper = new ObjectMapper();
    try {//from  ww  w  .j  a  v  a  2  s.co m
        while (cursor.hasNext()) {
            try {
                System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(cursor.next()));
            } catch (JsonProcessingException ex) {
                Logger.getLogger(SampleMongoDB.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } finally {
        cursor.close();
    }
}

From source file:com.inflight.rest.ViewPlaces.java

@SuppressWarnings("resource")
@GET/*from  www  .j a va 2 s . c o m*/
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public String getLocations() {

    JSONArray jsonArr = new JSONArray();

    MongoClientURI connectionString = new MongoClientURI(
            "mongodb://ramkrish:1234567@ds029446.mlab.com:29446/inflight");
    MongoClient mongoClient = new MongoClient(connectionString);

    MongoDatabase database = mongoClient.getDatabase("inflight");

    MongoCollection<Document> collection = database.getCollection("location");

    MongoCursor<Document> cursor = collection.find().iterator();
    try {
        while (cursor.hasNext()) {
            jsonArr.put(cursor.next());

        }
    } finally {
        cursor.close();
    }

    return jsonArr.toString();
}

From source file:com.jaeksoft.searchlib.crawler.database.DatabaseCrawlMongoDbThread.java

License:Open Source License

final private void runner_update(FindIterable<Document> iterable)
        throws SearchLibException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        IOException, ParseException, SyntaxError, URISyntaxException, InterruptedException {
    final int limit = databaseCrawl.getBufferSize();
    iterable.batchSize(limit);// w w  w. j ava2s.  c  o m
    DatabaseFieldMap databaseFieldMap = databaseCrawl.getFieldMap();
    List<IndexDocument> indexDocumentList = new ArrayList<IndexDocument>(0);
    LanguageEnum lang = databaseCrawl.getLang();
    FieldMapContext fieldMapContext = new FieldMapContext(client, lang);
    String uniqueField = client.getSchema().getUniqueField();
    MongoCursor<Document> cursor = iterable.iterator();
    while (cursor.hasNext() && !isAborted()) {

        String json = JSON.serialize(cursor.next());
        Object document = Configuration.defaultConfiguration().jsonProvider().parse(json);
        IndexDocument indexDocument = new IndexDocument(lang);
        databaseFieldMap.mapJson(fieldMapContext, document, indexDocument);
        if (uniqueField != null && !indexDocument.hasContent(uniqueField)) {
            rwl.w.lock();
            try {
                ignoredDocumentCount++;
            } finally {
                rwl.w.unlock();
            }
            continue;
        }
        indexDocumentList.add(indexDocument);
        rwl.w.lock();
        try {
            pendingIndexDocumentCount++;
        } finally {
            rwl.w.unlock();
        }
        if (index(indexDocumentList, limit))
            setStatus(CrawlStatus.CRAWL);

    }
    index(indexDocumentList, 0);
}

From source file:com.mycompany.mavenproject2.AddBrandController.java

@FXML

public void handleSaveCloseButton(ActionEvent a) throws UnknownHostException {
    ref = new BasicDBObject();
    ref.put("Name", Pattern.compile(BrandName.getText(), Pattern.CASE_INSENSITIVE));
    MongoCursor<Document> myDoc2 = col.find(ref).iterator();
    if (myDoc2.hasNext()) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error ");
        alert.setContentText("Brand already exist!");
        alert.showAndWait();//  w  w  w.j a  va  2s  .co m
        ManName.clear();
        BrandName.clear();
        repeat = 1;
    } else {
        repeat = 0;
    }
    if (repeat == 0) {
        InsertMongo();
        // InsertSQl(conn);
    }
    Stage stage = (Stage) Save.getScene().getWindow();
    stage.close();
}

From source file:com.mycompany.mavenproject2.AddBrandController.java

@FXML
public void handleSaveNewButton(ActionEvent a) throws UnknownHostException {
    ref = new BasicDBObject();
    ref.put("Name", Pattern.compile(BrandName.getText(), Pattern.CASE_INSENSITIVE));
    MongoCursor<Document> myDoc2 = col.find(ref).iterator();
    if (myDoc2.hasNext()) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error ");
        alert.setContentText("Brand already exist!");
        alert.showAndWait();/*from w w  w .  j a va  2  s .  c  om*/
        ManName.clear();
        BrandName.clear();
        repeat = 1;
    } else {
        repeat = 0;
    }
    if (repeat == 0) {
        InsertMongo();
        // InsertSQl();
    }

}

From source file:com.mycompany.mavenproject2.AddBrandController.java

public void InsertMongo() throws UnknownHostException {
    //MongoInsert           

    count = (int) col.count();
    if (count == 0) {
        seedData = createSeedData();//w  w  w . j a v a 2 s  . co  m
        col.insertOne(seedData);
    } else {
        sort1 = new BasicDBObject();
        MongoCursor<Document> cursor = col.find().sort(sort1).limit(1).skip((int) count - 1).iterator();
        try {
            while (cursor.hasNext()) {

                ID = cursor.next().getInteger("ID");
                System.out.println("last ID " + ID);
                ID++;
                System.out.println("Inserted " + ID);
            }
        } finally {
            cursor.close();
        }
        final Document seedData = createSeedData();
        col.insertOne(seedData);

    }

    for (Map.Entry<String, Object> entry : seedData.entrySet()) {
        System.out.println(entry.getKey() + "/" + entry.getValue());
    }
    BrandName.clear();
    ManName.clear();
}

From source file:com.mycompany.mavenproject2.AddCatController.java

@FXML
public void handleSaveNewButtonAction(ActionEvent a) throws UnknownHostException {
    ref = new BasicDBObject();
    ref.put("Name", Pattern.compile(CatName.getText(), Pattern.CASE_INSENSITIVE));
    MongoCursor<Document> myDoc2 = col.find(ref).iterator();
    if (myDoc2.hasNext()) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error ");
        alert.setContentText("Department already exist!");
        alert.showAndWait();/*from  w w w .  j a  va 2  s.  c o  m*/
        LocalName.clear();
        CatName.clear();
        repeat = 1;
    } else {
        repeat = 0;
    }
    if (repeat == 0) {
        InsertMongo();
        // InsertSQl(conn);
    }
}

From source file:com.mycompany.mavenproject2.AddCatController.java

@FXML
public void handleSavenCloseButton(ActionEvent a) {
    ref = new BasicDBObject();
    ref.put("Name", Pattern.compile(CatName.getText(), Pattern.CASE_INSENSITIVE));
    MongoCursor<Document> myDoc2 = col.find(ref).iterator();
    if (myDoc2.hasNext()) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error ");
        alert.setContentText("Category already exist!");
        alert.showAndWait();/*w w w .  j  a va2 s. c o m*/
        LocalName.clear();
        CatName.clear();
        repeat = 1;
    } else {
        repeat = 0;
    }
    if (repeat == 0) {
        InsertMongo();
        // InsertSQl(conn);
    }
    Stage stage = (Stage) Save.getScene().getWindow();
    stage.close();
}

From source file:com.mycompany.mavenproject2.AddCatController.java

public Document createSeedData() {
    d = new Document();
    b = new BasicDBObject();
    b.put("Name", DeptCombo.getValue());
    MongoCursor<Document> cursor = db.getCollection("DeptDetail").find(b).iterator();
    try {/*from  w  w w  .j a v a 2  s  .c o  m*/
        while (cursor.hasNext()) {
            i = cursor.next().getInteger("ID");
            System.out.println("Searched Output " + i);
        }
    } finally {
        cursor.close();
    }

    d.append("Name", CatName.getText());
    d.append("LocalName", LocalName.getText());
    d.append("Dept_ID", i);
    return d;
}

From source file:com.mycompany.mavenproject2.AddCatController.java

@Override
public void initialize(URL url, ResourceBundle rb) {

    /*   String qry="select name from dept_detail";
    java.sql.Connection conn=Connection.getConnect();
            //from   ww w .j a v  a2 s  .  c o m
    PreparedStatement pst2=conn.prepareStatement(qry);
    ResultSet rs1 = pst2.executeQuery(qry);
    while(rs1.next()){
       DeptCombo.setValue(rs1.getString("name"));   
       DeptCombo.getItems().addAll(rs1.getString("name"));            
    }       */
    MongoCursor<Document> cursor4 = db.getCollection("DeptDetail").find().iterator();
    try {
        while (cursor4.hasNext()) {

            String rs = cursor4.next().getString("Name");
            DeptCombo.setValue(rs);
            DeptCombo.getItems().addAll(rs);
            System.out.println("Intialize out put is " + rs);
        }
    } finally {
        cursor4.close();
    }

}