List of usage examples for com.mongodb DBCollection drop
public void drop()
From source file:org.eclipse.birt.report.engine.dataextraction.mongodb.MongoDataExtractionUtil.java
License:Open Source License
/** * @param mongoDbUrl/*from w w w .j a v a2 s .c o m*/ * @param mongodb * url mongoDbport mongodb port default 27017 mongoDb * @param mongodb * to connect to dBCollection defaulted to reportname * * @return DBCollection * @throws BirtException */ /* * Enhancements: Pass the opcode paramater to perform truncate & load or * Delta load */ public static DBCollection createCollection(String dB, String dBCollection) throws BirtException { DBCollection cursor = null; try { db = client.getDB(dB); cursor = db.getCollection(dBCollection); } catch (Exception e) { throw new BirtException(e.getMessage()); } /* * Truncate and load the data remove the below line to make it as delta * load to the collection */ cursor.drop(); return cursor; }
From source file:org.eclipselabs.emongo.example.ExampleComponent.java
License:Open Source License
public void activate() { DB database = mongoDatabaseProvider.getDB(); DBCollection collection = database.getCollection("items"); collection.drop(); for (int i = 0; i < 10; i++) collection.insert(new BasicDBObject("x", i)); DBCursor cursor = collection.find(); while (cursor.hasNext()) System.out.print(cursor.next().get("x") + " "); System.out.println();/*ww w. java2 s . c o m*/ collection.drop(); }
From source file:org.einherjer.week2.samples.DotNotationSample.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { Mongo client = new Mongo(); DB db = client.getDB("course"); DBCollection lines = db.getCollection("dotNotationSample"); lines.drop(); Random rand = new Random(); // insert 10 lines with random start and end points for (int i = 0; i < 10; i++) { lines.insert(/*from w ww . java 2 s . co m*/ new BasicDBObject("_id", i) .append("start", new BasicDBObject("x", rand.nextInt(90) + 10).append("y", rand.nextInt(90) + 10)) .append("end", new BasicDBObject("x", rand.nextInt(90) + 10).append("y", rand.nextInt(90) + 10))); } QueryBuilder builder = QueryBuilder.start("start.x").greaterThan(50); DBCursor cursor = lines.find(builder.get(), new BasicDBObject("start.y", true).append("_id", false)); try { while (cursor.hasNext()) { DBObject cur = cursor.next(); System.out.println(cur); } } finally { cursor.close(); } }
From source file:org.einherjer.week2.samples.FieldSelectionSample.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { Mongo client = new Mongo(); DB db = client.getDB("course"); DBCollection collection = db.getCollection("fieldSelectionSample"); collection.drop(); Random rand = new Random(); // insert 10 documents with two random integers for (int i = 0; i < 10; i++) { collection.insert(new BasicDBObject("x", rand.nextInt(2)).append("y", rand.nextInt(100)).append("z", rand.nextInt(1000)));/* ww w .j a va2 s.c o m*/ } DBObject query = QueryBuilder.start("x").is(0).and("y").greaterThan(10).lessThan(70).get(); //use second find parameter to filter the returned fields DBCursor cursor = collection.find(query, new BasicDBObject("y", true).append("_id", false)); try { while (cursor.hasNext()) { DBObject cur = cursor.next(); System.out.println(cur); } } finally { cursor.close(); } }
From source file:org.einherjer.week2.samples.FindAndModifySample.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { DBCollection collection = createCollection(); collection.drop(); final String counterId = "abc"; int first;/* w w w. j av a2s . c om*/ int numNeeded; numNeeded = 2; first = getRange(counterId, numNeeded, collection); System.out.println("Range: " + first + "-" + (first + numNeeded - 1)); numNeeded = 3; first = getRange(counterId, numNeeded, collection); System.out.println("Range: " + first + "-" + (first + numNeeded - 1)); numNeeded = 10; first = getRange(counterId, numNeeded, collection); System.out.println("Range: " + first + "-" + (first + numNeeded - 1)); System.out.println(); printCollection(collection); }
From source file:org.einherjer.week2.samples.FindCriteriaSample.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { Mongo client = new Mongo(); DB db = client.getDB("course"); DBCollection collection = db.getCollection("findCriteriaSample"); collection.drop(); // insert 10 documents with two random integers for (int i = 0; i < 10; i++) { collection/* ww w . j a v a2s.com*/ .insert(new BasicDBObject("x", new Random().nextInt(2)).append("y", new Random().nextInt(100))); } //1- The query document can be created by using a QueryBuilder... QueryBuilder builder = QueryBuilder.start("x").is(0).and("y").greaterThan(10).lessThan(70); //2- or, the query document can be created manually DBObject query = new BasicDBObject("x", 0).append("y", new BasicDBObject("$gt", 10).append("$lt", 90)); System.out.println("\nCount:"); long count = collection.count(builder.get()); System.out.println(count); System.out.println("\nFind all: "); DBCursor cursor = collection.find(builder.get()); try { while (cursor.hasNext()) { DBObject cur = cursor.next(); System.out.println(cur); } } finally { cursor.close(); } }
From source file:org.einherjer.week2.samples.FindSample.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { Mongo client = new Mongo(); DB db = client.getDB("course"); DBCollection collection = db.getCollection("findSample"); collection.drop(); // insert 10 documents with a random integer as the value of field "x" for (int i = 0; i < 10; i++) { collection.insert(new BasicDBObject("x", new Random().nextInt(100))); }/*from ww w . jav a 2s . c o m*/ System.out.println("Find one:"); DBObject one = collection.findOne(); System.out.println(one); System.out.println("\nFind all: "); DBCursor cursor = collection.find(); try { while (cursor.hasNext()) { DBObject cur = cursor.next(); System.out.println(cur); } } finally { cursor.close(); } System.out.println("\nCount:"); long count = collection.count(); System.out.println(count); }
From source file:org.einherjer.week2.samples.InsertSample.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { Mongo client = new Mongo(); DB courseDB = client.getDB("course"); DBCollection collection = courseDB.getCollection("insertSample"); collection.drop(); DBObject doc = new BasicDBObject().append("x", 1); System.out.println(doc);//from www . j a v a2s . co m collection.insert(doc); System.out.println(doc); //_id field gets generated DBObject doc2 = new BasicDBObject("_id", new ObjectId()).append("x", 1); System.out.println(doc2); collection.insert(doc2); System.out.println(doc2); //same as doc1 DBObject doc3 = new BasicDBObject("_id", "123").append("x", 1); System.out.println(doc3); collection.insert(doc3); System.out.println(doc3); //_id field set by to a type different than ObjectId collection.insert(doc); //duplicate exception }
From source file:org.einherjer.week2.samples.SortSkipLimitSample.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { Mongo client = new Mongo(); DB db = client.getDB("course"); DBCollection lines = db.getCollection("sortSkipLimitSample"); lines.drop(); Random rand = new Random(); // insert 10 lines with random start and end points for (int i = 0; i < 10; i++) { lines.insert(new BasicDBObject("_id", i) .append("start", new BasicDBObject("x", rand.nextInt(2)).append("y", rand.nextInt(90) + 10)) .append("end", new BasicDBObject("x", rand.nextInt(2)).append("y", rand.nextInt(90) + 10))); }/* w ww.ja v a 2s . c o m*/ DBCursor cursor = lines.find().sort(new BasicDBObject("start.x", 1).append("start.y", -1)).skip(2) .limit(10); try { while (cursor.hasNext()) { DBObject cur = cursor.next(); System.out.println(cur); } } finally { cursor.close(); } }
From source file:org.einherjer.week2.samples.UpdateRemoveSample.java
License:Apache License
private static DBCollection createCollection() throws UnknownHostException { Mongo client = new Mongo(); DB db = client.getDB("course"); DBCollection collection = db.getCollection("updateRemoveSample"); collection.drop(); return collection; }