Permalink
Browse files

Modernized examples

  • Loading branch information...
1 parent a38b467 commit b2182e60061f8870a3bc655a7adaa1b173569142 @jyemin jyemin committed Nov 18, 2012
Showing with 85 additions and 107 deletions.
  1. +37 −55 examples/QuickTour.java
  2. +13 −14 examples/QuickTourAdmin.java
  3. +35 −38 examples/ReadOplog.java
View
@@ -1,5 +1,5 @@
/**
- * Copyright (C) 2008 10gen Inc.
+ * Copyright (C) 2008-2012 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,74 +14,59 @@
* limitations under the License.
*/
-import com.mongodb.Mongo;
-import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
-import com.mongodb.DBObject;
-import com.mongodb.DBCursor;
import com.mongodb.DB;
-import com.mongodb.WriteConcern;
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.MongoClient;
-import java.util.Set;
import java.util.List;
+import java.util.Set;
public class QuickTour {
public static void main(String[] args) throws Exception {
// connect to the local database server
- Mongo m = new Mongo();
-
- m.setWriteConcern(WriteConcern.SAFE);
+ MongoClient mongoClient = new MongoClient();
// get handle to "mydb"
- DB db = m.getDB( "mydb" );
-
+ DB db = mongoClient.getDB("mydb");
+
// Authenticate - optional
// boolean auth = db.authenticate("foo", "bar");
-
// get a list of the collections in this database and print them out
- Set<String> colls = db.getCollectionNames();
- for (String s : colls) {
+ Set<String> collectionNames = db.getCollectionNames();
+ for (String s : collectionNames) {
System.out.println(s);
}
// get a collection object to work with
- DBCollection coll = db.getCollection("testCollection");
-
- // drop all the data in it
- coll.drop();
+ DBCollection testCollection = db.getCollection("testCollection");
+ // drop all the data in it
+ testCollection.drop();
// make a document and insert it
- BasicDBObject doc = new BasicDBObject();
-
- doc.put("name", "MongoDB");
- doc.put("type", "database");
- doc.put("count", 1);
-
- BasicDBObject info = new BasicDBObject();
+ BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1)
+ .append("info", new BasicDBObject("x", 203).append("y", 102));
- info.put("x", 203);
- info.put("y", 102);
-
- doc.put("info", info);
-
- coll.insert(doc);
+ testCollection.insert(doc);
// get it (since it's the only one in there since we dropped the rest earlier on)
- DBObject myDoc = coll.findOne();
+ DBObject myDoc = testCollection.findOne();
System.out.println(myDoc);
// now, lets add lots of little documents to the collection so we can explore queries and cursors
- for (int i=0; i < 100; i++) {
- coll.insert(new BasicDBObject().append("i", i));
+ for (int i = 0; i < 100; i++) {
+ testCollection.insert(new BasicDBObject().append("i", i));
}
- System.out.println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount());
+ System.out.println("total # of documents after inserting 100 small ones (should be 101) " + testCollection.getCount());
// lets get all the documents in the collection and print them out
- DBCursor cursor = coll.find();
+ DBCursor cursor = testCollection.find();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
@@ -91,50 +76,47 @@ public static void main(String[] args) throws Exception {
}
// now use a query to get 1 document out
- BasicDBObject query = new BasicDBObject();
- query.put("i", 71);
- cursor = coll.find(query);
+ BasicDBObject query = new BasicDBObject("i", 71);
+ cursor = testCollection.find(query);
try {
- while(cursor.hasNext()) {
+ while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
// now use a range query to get a larger subset
- query = new BasicDBObject();
- query.put("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50
- cursor = coll.find(query);
+ query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50
+ cursor = testCollection.find(query);
try {
- while(cursor.hasNext()) {
- System.out.println(cursor.next());
- }
+ while (cursor.hasNext()) {
+ System.out.println(cursor.next());
+ }
} finally {
cursor.close();
}
- // range query with multiple contstraings
- query = new BasicDBObject();
- query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30
- cursor = coll.find(query);
+ // range query with multiple constraints
+ query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30
+ cursor = testCollection.find(query);
try {
- while(cursor.hasNext()) {
+ while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
// create an index on the "i" field
- coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending
+ testCollection.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending
// list the indexes on the collection
- List<DBObject> list = coll.getIndexInfo();
+ List<DBObject> list = testCollection.getIndexInfo();
for (DBObject o : list) {
System.out.println(o);
}
@@ -154,6 +136,6 @@ public static void main(String[] args) throws Exception {
db.resetError();
// release resources
- m.close();
+ mongoClient.close();
}
}
@@ -1,5 +1,5 @@
/**
- * Copyright (C) 2008 10gen Inc.
+ * Copyright (C) 2008-2012 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,41 +14,40 @@
* limitations under the License.
*/
-import com.mongodb.DB;
-import com.mongodb.Mongo;
import com.mongodb.BasicDBObject;
+import com.mongodb.DB;
+import com.mongodb.MongoClient;
public class QuickTourAdmin {
-
+
public static void main(String[] args) throws Exception {
// connect to the local database server
- Mongo m = new Mongo();
+ MongoClient mongoClient = new MongoClient();
// Authenticate - optional
// boolean auth = db.authenticate("foo", "bar");
-
+
// get db names
- for (String s : m.getDatabaseNames()) {
+ for (String s : mongoClient.getDatabaseNames()) {
System.out.println(s);
}
-
// get a db
- DB db = m.getDB("com_mongodb_MongoAdmin");
+ DB db = mongoClient.getDB("com_mongodb_MongoAdmin");
// do an insert so that the db will really be created. Calling getDB() doesn't really take any
// action with the server
- db.getCollection("testcollection").insert(new BasicDBObject("i",1));
- for (String s : m.getDatabaseNames()) {
+ db.getCollection("testcollection").insert(new BasicDBObject("i", 1));
+ for (String s : mongoClient.getDatabaseNames()) {
System.out.println(s);
}
// drop a database
- m.dropDatabase("com_mongodb_MongoAdmin");
+ mongoClient.dropDatabase("com_mongodb_MongoAdmin");
- for (String s : m.getDatabaseNames()) {
+ for (String s : mongoClient.getDatabaseNames()) {
System.out.println(s);
- }
+ }
}
}
View
@@ -1,7 +1,5 @@
-// ReadOplog.java
-
/**
- * Copyright (C) 2008 10gen Inc.
+ * Copyright (C) 2008-2012 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,47 +14,46 @@
* limitations under the License.
*/
-package examples;
-
-import com.mongodb.*;
-import org.bson.types.*;
-import java.util.*;
+import com.mongodb.BasicDBObject;
+import com.mongodb.Bytes;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.MongoClient;
+import org.bson.types.BSONTimestamp;
public class ReadOplog {
- public static void main(String[] args)
- throws Exception {
-
- Mongo m = new Mongo();
- DB local = m.getDB( "local" );
-
- DBCollection oplog = local.getCollection( "oplog.$main" );
-
- DBObject last = null;
- {
- DBCursor lastCursor = oplog.find().sort( new BasicDBObject( "$natural" , -1 ) ).limit(1);
- if ( ! lastCursor.hasNext() ){
- System.out.println( "no oplog!" );
- return;
- }
- last = lastCursor.next();
+ public static void main(String[] args) throws Exception {
+
+ MongoClient mongoClient = new MongoClient();
+ DB local = mongoClient.getDB("local");
+
+ DBCollection oplog = local.getCollection("oplog.$main");
+
+ DBCursor lastCursor = oplog.find().sort(new BasicDBObject("$natural", -1)).limit(1);
+ if (!lastCursor.hasNext()) {
+ System.out.println("no oplog!");
+ return;
}
-
- BSONTimestamp ts = (BSONTimestamp)last.get("ts");
- System.out.println( "starting point: " + ts );
-
- while ( true ){
- System.out.println( "starting at ts: " + ts );
- DBCursor cursor = oplog.find( new BasicDBObject( "ts" , new BasicDBObject( "$gt" , ts ) ) );
- cursor.addOption( Bytes.QUERYOPTION_TAILABLE );
- cursor.addOption( Bytes.QUERYOPTION_AWAITDATA );
- while ( cursor.hasNext() ){
+ DBObject last = lastCursor.next();
+
+ BSONTimestamp ts = (BSONTimestamp) last.get("ts");
+ System.out.println("starting point: " + ts);
+
+ while (true) {
+ System.out.println("starting at ts: " + ts);
+ DBCursor cursor = oplog.find(new BasicDBObject("ts", new BasicDBObject("$gt", ts)));
+ cursor.addOption(Bytes.QUERYOPTION_TAILABLE);
+ cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
+ while (cursor.hasNext()) {
DBObject x = cursor.next();
- ts = (BSONTimestamp)x.get("ts");
- System.out.println( "\t" + x );
+ ts = (BSONTimestamp) x.get("ts");
+ System.out.println("\t" + x);
}
-
- Thread.sleep( 1000 );
+
+ Thread.sleep(1000);
}
}
}

0 comments on commit b2182e6

Please sign in to comment.