List of usage examples for com.mongodb MongoClient setWriteConcern
@Deprecated public void setWriteConcern(final WriteConcern writeConcern)
From source file:org.axonframework.mongo.eventsourcing.eventstore.MongoFactory.java
License:Apache License
/** * Creates a mongo instance based on the provided configuration. Read javadoc of the class to learn about the * configuration options. A new Mongo instance is created each time this method is called. * * @return a new Mongo instance each time this method is called. *///from w w w. j a v a 2s. co m public MongoClient createMongo() { MongoClient mongo; if (mongoAddresses.isEmpty()) { mongo = new MongoClient(new ServerAddress(), mongoOptions); } else { mongo = new MongoClient(mongoAddresses, mongoOptions); } mongo.setWriteConcern(defaultWriteConcern()); return mongo; }
From source file:org.jongo.util.MongoResource.java
License:Apache License
private static MongoClient createClient(int port) throws UnknownHostException { MongoClient mongo = new MongoClient("127.0.0.1", port); mongo.setWriteConcern(WriteConcern.FSYNC_SAFE); return mongo; }
From source file:tourapi.TourAPI.java
private void MLUpdate(String from, String to) { // ?/*w w w . jav a 2 s . c o m*/ //fromt->to MongoClient mongoClient = getMongoClient(); String result = null; try { DB db = mongoClient.getDB("my_database"); DBCollection coll = db.getCollection("ML_Result"); WriteConcern w = new WriteConcern(1, 0); mongoClient.setWriteConcern(w); DBObject doc = new BasicDBObject(); // string? select BasicDBObject query = new BasicDBObject(); query.put("from", from); query.put("to", to); DBCursor cursor = coll.find(query); if (cursor.hasNext())// ?? { DBObject mapObj = cursor.next(); int num = ((Number) mapObj.get("num")).intValue(); num++; BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", new BasicDBObject().append("num", num)); BasicDBObject searchQuery = new BasicDBObject().append("from", from).append("to", to); coll.update(searchQuery, newDocument); cursor.close(); } else {// ?? ? -> ?? insert doc.put("from", from); doc.put("to", to); doc.put("num", 1); coll.insert(doc); cursor.close(); } System.out.println(coll.getCount()); // close resources mongoClient.close(); } catch (Exception e) { System.err.println("error evoke"); System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }
From source file:tourapi.TourAPI.java
private String convertTitleToId(String title, String url, int num) { String result = null;//from ww w . j a va 2 s . c o m try { // To connect to mongodb server MongoClient mongoClient = getMongoClient(); DB db = mongoClient.getDB("my_database"); DBCollection coll = db.getCollection("TB_titleID"); System.out.println("? "); WriteConcern w = new WriteConcern(1, 0); mongoClient.setWriteConcern(w); // Now connect to your databases DBObject doc = new BasicDBObject(); BasicDBObject query = new BasicDBObject("title", title); DBCursor cursor = coll.find(query); if (cursor.hasNext())// ?? { result = cursor.next().get("ID").toString(); cursor.close(); } else {// ?? ? -> ?? insert int autoIncre = (int) coll.getCount(); doc.put("ID", autoIncre); doc.put("title", title); System.out.println("URL : " + url); doc.put("url", url); doc.put("num", num); coll.insert(doc); cursor.close(); result = autoIncre + ""; } // close resources mongoClient.close(); } catch (Exception e) { System.err.println("convertTitleToId error"); System.err.println(e.getClass().getName() + ": " + e.getMessage()); } return result; }
From source file:tourapi.TourAPI.java
/** * @param args the command line arguments *//*from ww w.ja va2 s . c o m*/ private tour_Information findwithDB(String from) { tour_Information returntour = new tour_Information(); String to = null; try { MongoClient mongoClient = getMongoClient(); DB db = mongoClient.getDB("my_database"); DBCollection coll = db.getCollection("ML_Result"); WriteConcern w = new WriteConcern(1, 0); mongoClient.setWriteConcern(w); // string? select BasicDBObject query = new BasicDBObject(); query.put("from", from); DBCursor cursor = coll.find(query); cursor.sort(new BasicDBObject("num", -1)); int num = -1; System.out.println("from 0: " + from); if (cursor.hasNext())// ?? { DBObject mapObj = cursor.next(); String temp = mapObj.get("to").toString(); num = Integer.parseInt(temp); //num = ((Number) mapObj.get("to")).intValue(); cursor.close(); } else { //System.out.println("from2 : "+from); returntour.myURL = null; } System.out.println(coll.getCount()); // close resources if (num != -1) { db = mongoClient.getDB("my_database"); coll = db.getCollection("TB_titleID"); w = new WriteConcern(1, 0);// ? , 2000 // ? 2 ?? ? ? mongoClient.setWriteConcern(w); // string? select query = new BasicDBObject(); // query.put("ID", num); //query.put("to",to); cursor = coll.find(query); if (cursor.hasNext())// ?? { System.out.println("num : " + num); DBObject mapObj = cursor.next(); returntour.myURL = mapObj.get("url").toString(); returntour.myLocation = Integer.parseInt(mapObj.get("num").toString()); cursor.close(); } else { returntour.myURL = null; returntour.myLocation = 0; } } mongoClient.close(); } catch (Exception e) { System.err.println("findwithDB Exception"); System.err.println(e.getClass().getName() + ": " + e.getMessage()); } return returntour; }