Example usage for com.mongodb.client MongoCursor next

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

Introduction

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

Prototype

@Override
    TResult next();

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 w  w w.ja  v  a 2  s  .  c o 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/*  www  .ja  v  a2 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);//from   ww w  .j a  va  2s. c om
    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

public void InsertMongo() throws UnknownHostException {
    //MongoInsert           

    count = (int) col.count();
    if (count == 0) {
        seedData = createSeedData();/*from www .  jav 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

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. ja v  a2s .co  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 w w  w.  ja v a2  s  .  com
    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();
    }

}

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

public void InsertMongo() throws ParseException, IOException {
    if (Margin.isSelected()) {
        rFlag1 = 1;/* www .ja  v a2 s .c  om*/
    } else {
        rFlag1 = 0;
    }
    if (MarkUp.isSelected()) {
        rFlag2 = 1;
    } else {
        rFlag2 = 0;
    }
    if (Taxable.isSelected()) {
        tFlag = 1;
    } else {
        tFlag = 0;
    }
    if (Price.isSelected()) {
        pFlag = 1;
    } else {
        pFlag = 0;
    }
    if (FoodStamp.isSelected()) {
        fFlag = 1;
    } else {
        fFlag = 0;
    }

    /* DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
     db.getCollection("restaurants").insertOne(  new Document("address",
                 new Document()
                         .append("street", "2 Avenue")
                         .append("zipcode", "10075")
                         .append("building", "1480")
                         .append("coord", asList(-73.9557413, 40.7720266)))
                 .append("borough", "Manhattan")
                 .append("cuisine", "Italian")
                 .append("grades", asList(
                         new Document()
                                 .append("date", format.parse("2014-10-01T00:00:00Z"))
                                 .append("grade", "A")
                                 .append("score", 11),
                         new Document()
                                 .append("date", format.parse("2014-01-16T00:00:00Z"))
                                 .append("grade", "B")
                                 .append("score", 17)))
                 .append("name", "Vella")
                 .append("restaurant_id", "41704620"));
     System.out.println("Data are Inserted");*/
    //Find all document
    /*b=new BasicDBObject();
    b.put("Name", "sdfg");
    MongoCursor<Document> cursor = db.getCollection("CategoryDetail").find(b).iterator();
    try {
    while (cursor.hasNext()) {
        System.out.println("Searched Output "+cursor.next().toJson() );
                
    }
    } finally {
    cursor.close();
    }*/
    /* MongoCursor<Document> cursor4 = db.getCollection("CategoryDetail").find().iterator();
      try {
    while (cursor4.hasNext()) {
        System.out.println("column val is "+cursor4.next().getString("LocalName"));
                
    }
     } finally {
    cursor.close();
     }*/
    //Find First Document

    /*   Document myDoc = db.getCollection("restaurants").find().first();
       System.out.println("First Column is "+myDoc.toJson());*/
    //Where Clause
    /*Document myDoc = db.getCollection("restaurants").find(eq("borough", "ManhattanFirst")).first();
    System.out.println("sd" +myDoc.toJson());*/
    //col.find({}).sort({_id:-1}).limit(1);
    count = (int) col.count();
    if (count == 0) {
        seedData = createSeedData();
        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);

    }
    /*count= (long) col.count();
    MongoCursor<Document> cursor = col.find().sort(sort1).limit(1).skip((int)count-1).iterator();        
    try {
    while (cursor.hasNext()) {
        System.out.println("Searched Output "+cursor.next().toJson());
    }
    } finally {
    cursor.close();
    }*/

    /* MongoCursor<Document> cursor = col.find().limit(3).sort({timestamp:-1}); 
     try {
    while (cursor.hasNext()) {
        System.out.println("Searched Output "+cursor.next().toJson());
    }
     } finally {
    cursor.close();
     }*/
    Stage stage = new Stage();
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/SelectDept.fxml"));
    Parent root4 = (Parent) fxmlLoader.load();
    SelectDeptController controller = fxmlLoader.<SelectDeptController>getController();
    stage.setScene(new Scene(root4));
    stage.setTitle("Select Department");
    controller.update();
    LocalName.clear();
    DeptName.clear();
    value.clear();
}

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

public void EditButton(String Name) {
    obv.removeAll(obv);/*from   w  w  w.j  a v a 2 s.  co  m*/
    grp = Name;
    GroupNameText.setText(Name);
    ItemTable2.getItems().removeAll(obv);
    GroupNameText.setDisable(true);
    System.out.println("Cllaed " + Name);
    GlobalFlag = 1;
    db1.put("GroupName", Name);
    MongoCursor<Document> cursorFind = db.getCollection("ItemGroup").find(db1).iterator();
    MongoCursor<Document> cursorFind1 = db.getCollection("ItemDetail").find().iterator();
    while (cursorFind.hasNext()) {
        Document g = cursorFind.next();
        GroupDescText.setText(g.getString("GroupDesc"));
        Document d = (Document) g.get("Items");
        while (cursorFind1.hasNext()) {
            Document g1 = cursorFind1.next();
            if (d.getString(g1.getString("SKU")) != null) {

                obv.add(new Person(g1.getString("SKU"), g1.getString("ItemDesc"), g1.getString("Size_Name"),
                        g1.getString("Pack_Name"), g1.getString("UnitPrice")));
                ItemTable2.setItems(obv);
            }
        }

    }
}

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

@Override
public void initialize(URL url, ResourceBundle rb) {
    skucol.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("SKU"));
    desc.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("Desc"));
    size.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("Size"));
    pack.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("Pack"));
    price.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("Price"));
    sku.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("SKU"));
    desccol.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("Desc"));
    sizecol.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("Size"));
    packcol.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("Pack"));
    pricecol.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person, String>("Price"));
    data.removeAll(data);/*  ww  w  .ja  va2 s . c o  m*/
    ItemTable.getItems().removeAll(data);
    MongoCursor<Document> cursorFind = db.getCollection("ItemDetail").find().iterator();
    while (cursorFind.hasNext()) {
        Document g = cursorFind.next();
        data.add(new Person(g.getString("SKU"), g.getString("ItemDesc"), g.getString("Size_Name"),
                g.getString("Pack_Name"), g.getString("UnitCost")));
    }
    ItemTable.setItems(data);
    /*  MongoCursor<Document> cur = db.getCollection("CategoryDetail").find().iterator();
      CatCombo.getItems().clear();
    try {
    while (cur.hasNext()) {                
        String rs1=cur.next().getString("Name");              
        CatCombo.setValue(rs1);
        CatCombo.getItems().addAll(rs1);           
    }             
      } finally {
    cur.close();
      }   
      cur =  db.getCollection("DeptDetail").find().iterator();
    try {
    while (cur.hasNext()) {                
        String rs=cur.next().getString("Name");
        DeptCombo.setValue(rs);
        DeptCombo.getItems().addAll(rs);                
    }
      } finally {
    cur.close();
      }     
     cur =  db.getCollection("BrandDetail").find().iterator();
    try {
    while (cur.hasNext()) {                
        String rs=cur.next().getString("Name");
        BrandCombo.setValue(rs);
        BrandCombo.getItems().addAll(rs);                
    }
      } finally {
    cur.close();
      }   
      cur =  db.getCollection("PackDetail").find().iterator();
    try {
        while (cur.hasNext()) {                
        String rs=cur.next().getString("Name");
        PackCombo.setValue(rs);
        PackCombo.getItems().addAll(rs);                
    }
      } finally {
    cur.close();
      }   
      cur =  db.getCollection("SizeDetail").find().iterator();
    try {
    while (cur.hasNext()) {                
        String rs=cur.next().getString("Name");
        SizeCombo.setValue(rs);
        SizeCombo.getItems().addAll(rs);                
    }
      } finally {
    cur.close();
      }*/
    filterField.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

            //updateFilteredData();
            initFilter();
        }
    });
}

From source file:com.mycompany.mavenproject2.AddItemListController.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 . ja  va2  s. c o  m*/
        while (cursor.hasNext()) {
            dept_id = cursor.next().getInteger("ID");
            System.out.println("Searched Output " + dept_id);
        }
    } finally {
        cursor.close();
    }

    b.clear();
    b.put("Name", SizeCombo.getValue().toString());
    System.out.println("Size outtput is " + SizeCombo.getValue().toString());
    cursor = db.getCollection("SizeDetail").find(b).iterator();
    try {
        while (cursor.hasNext()) {
            size_name = cursor.next().getString("Name");
            System.out.println("size Output " + size_name);

        }
    } finally {
        cursor.close();
    }
    b.clear();
    b.put("Name", PackCombo.getValue().toString());
    cursor = db.getCollection("PackDetail").find(b).iterator();
    try {
        while (cursor.hasNext()) {
            pack_name = cursor.next().getString("Name");
            System.out.println("pack Output " + pack_name);
        }
    } finally {
        cursor.close();
    }
    b.clear();
    b.put("Name", BrandCombo.getValue().toString());
    cursor = db.getCollection("BrandDetail").find(b).iterator();
    try {
        while (cursor.hasNext()) {
            brand_name = cursor.next().getString("Name");
            System.out.println("brand Output " + brand_name);
        }
    } finally {
        cursor.close();
    }
    if (NonStockCheck.isSelected()) {
        nonstock = 1;
    } else {
        nonstock = 0;
    }
    if (NonTaxCheck.isSelected()) {
        nontax = 1;
    } else {
        nontax = 0;
    }
    if (QuantityCheck.isSelected()) {
        qua = 1;
    } else {
        qua = 0;
    }
    if (PriceCheck.isSelected()) {
        priceF = 1;
    } else {
        priceF = 0;
    }
    if (WeightedItem.isSelected()) {
        check1 = 1;
    } else {
        check1 = 0;
    }
    if (WebItem.isSelected()) {
        check2 = 1;
    } else {
        check2 = 0;
    }
    if (ExcludeSale.isSelected()) {
        check3 = 1;
    } else {
        check3 = 0;
    }
    if (WIC.isSelected()) {
        check4 = 1;
    } else {
        check4 = 0;
    }
    if (HealthCard.isSelected()) {
        check5 = 1;
    } else {
        check5 = 0;
    }
    if (FoodStamp.isSelected()) {
        check6 = 1;
    } else {
        check6 = 0;
    }
    if (NonRevenueItem.isSelected()) {
        check7 = 1;
    } else {
        check7 = 0;
    }
    if (NonDiscountable.isSelected()) {
        check8 = 1;
    } else {
        check8 = 0;
    }

    d.append("SKU", SKUTextField.getText());
    d.append("ItemDesc", DescriptionTextField.getText());
    d.append("ItemName", ItemNameText.getText());
    d.append("Dept_ID", dept_id);
    d.append("Size_Name", size_name);
    d.append("Pack_Name", pack_name);
    d.append("Cat_Name", CatCombo.getValue());
    d.append("Brand_Name", brand_name);
    d.append("Location", LocationCombo.getValue());
    d.append("Model_Num", ModelText.getText());
    d.append("Part_Num", PartText.getText());
    d.append("NonStock", nonstock);
    d.append("UnitType", IventoryTypeCombo.getValue());
    d.append("NonText", nontax);
    d.append("Tax1", Tax1Text.getText());
    d.append("Tax2", Tax2Text.getText());
    d.append("Tax3", Tax3Text.getText());
    d.append("QualityPrompt", priceF);
    d.append("PricePrompt", priceF);
    d.append("UnitCost", UnitCostText.getText());
    d.append("UnitPrice", UnitPriceText.getText());
    d.append("Margin", MarginText.getText());
    d.append("MSRP", MSRPText.getText());
    d.append("BuyDownAmount", BuyDownText.getText());
    d.append("MarkUP", MarkUpText.getText());
    d.append("SalesPrice", SaleText.getText());
    d.append("WeightedItem", check1);
    d.append("WebItem", check2);
    d.append("ExcludeSale", check3);
    d.append("WIC", check4);
    d.append("HealthCard", check5);
    d.append("FoodStamp", check6);
    d.append("NonRevenueItem", check7);
    d.append("NonDiscountable", check8);
    table.getItems().forEach(item -> d.append("Quantity", item.getValue()));
    costTable.getItems().forEach(item -> d.append("CostQuantity", item.getUnitName()));
    costTable.getItems().forEach(item -> d.append("Price", item.getValue()));
    return d;
}