List of usage examples for com.mongodb WriteConcern WriteConcern
public WriteConcern(final int w, final int wTimeoutMS)
From source file:org.opencb.commons.datastore.mongodb.MongoDBNativeQuery.java
License:Apache License
/** * This method insert a single document into a collection. Params w and wtimeout are read from QueryOptions. * * @param document The new document to be inserted * @param options Some options like timeout *//*from w ww. j av a2 s .com*/ public void insert(Document document, QueryOptions options) { int writeConcern = 1; int ms = 0; if (options != null && (options.containsKey("w") || options.containsKey("wtimeout"))) { // Some info about params: http://api.mongodb.org/java/current/com/mongodb/WriteConcern.html // return dbCollection.insert(dbObject, new WriteConcern(options.getInt("w", 1), options.getInt("wtimeout", 0))); writeConcern = options.getInt("w", 1); ms = options.getInt("wtimeout", 0); } dbCollection.withWriteConcern(new WriteConcern(writeConcern, ms)); dbCollection.insertOne(document); }
From source file:org.opencb.commons.datastore.mongodb.MongoDBNativeQuery.java
License:Apache License
/** * This method insert a list of documents into a collection. Params w and wtimeout are read from QueryOptions. * * @param documentList The new list of documents to be inserted * @param options Some options like timeout * @return A BulkWriteResult from MongoDB API *///from ww w. j a v a2s. co m public BulkWriteResult insert(List<Document> documentList, QueryOptions options) { List<WriteModel<Document>> actions = new ArrayList<>(documentList.size()); for (Document document : documentList) { actions.add(new InsertOneModel<>(document)); } int writeConcern = 1; int ms = 0; if (options != null && (options.containsKey("w") || options.containsKey("wtimeout"))) { writeConcern = options.getInt("w", 1); ms = options.getInt("wtimeout", 0); } dbCollection.withWriteConcern(new WriteConcern(writeConcern, ms)); return dbCollection.bulkWrite(actions, new BulkWriteOptions().ordered(false)); }
From source file:tourapi.TourAPI.java
private void MLUpdate(String from, String to) { // ?/*from w w w . j a v a2s. 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;/* w w 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 www.ja v a2 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; }