Skip to content

Commit b2182e6

Browse files
committed
Modernized examples
1 parent a38b467 commit b2182e6

File tree

3 files changed

+85
-107
lines changed

3 files changed

+85
-107
lines changed

examples/QuickTour.java

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (C) 2008 10gen Inc.
2+
* Copyright (C) 2008-2012 10gen Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,74 +14,59 @@
1414
* limitations under the License.
1515
*/
1616

17-
import com.mongodb.Mongo;
18-
import com.mongodb.DBCollection;
1917
import com.mongodb.BasicDBObject;
20-
import com.mongodb.DBObject;
21-
import com.mongodb.DBCursor;
2218
import com.mongodb.DB;
23-
import com.mongodb.WriteConcern;
19+
import com.mongodb.DBCollection;
20+
import com.mongodb.DBCursor;
21+
import com.mongodb.DBObject;
22+
import com.mongodb.MongoClient;
2423

25-
import java.util.Set;
2624
import java.util.List;
25+
import java.util.Set;
2726

2827
public class QuickTour {
2928

3029
public static void main(String[] args) throws Exception {
3130

3231
// connect to the local database server
33-
Mongo m = new Mongo();
34-
35-
m.setWriteConcern(WriteConcern.SAFE);
32+
MongoClient mongoClient = new MongoClient();
3633

3734
// get handle to "mydb"
38-
DB db = m.getDB( "mydb" );
39-
35+
DB db = mongoClient.getDB("mydb");
36+
4037
// Authenticate - optional
4138
// boolean auth = db.authenticate("foo", "bar");
4239

43-
4440
// get a list of the collections in this database and print them out
45-
Set<String> colls = db.getCollectionNames();
46-
for (String s : colls) {
41+
Set<String> collectionNames = db.getCollectionNames();
42+
for (String s : collectionNames) {
4743
System.out.println(s);
4844
}
4945

5046
// get a collection object to work with
51-
DBCollection coll = db.getCollection("testCollection");
52-
53-
// drop all the data in it
54-
coll.drop();
47+
DBCollection testCollection = db.getCollection("testCollection");
5548

49+
// drop all the data in it
50+
testCollection.drop();
5651

5752
// make a document and insert it
58-
BasicDBObject doc = new BasicDBObject();
59-
60-
doc.put("name", "MongoDB");
61-
doc.put("type", "database");
62-
doc.put("count", 1);
63-
64-
BasicDBObject info = new BasicDBObject();
53+
BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1)
54+
.append("info", new BasicDBObject("x", 203).append("y", 102));
6555

66-
info.put("x", 203);
67-
info.put("y", 102);
68-
69-
doc.put("info", info);
70-
71-
coll.insert(doc);
56+
testCollection.insert(doc);
7257

7358
// get it (since it's the only one in there since we dropped the rest earlier on)
74-
DBObject myDoc = coll.findOne();
59+
DBObject myDoc = testCollection.findOne();
7560
System.out.println(myDoc);
7661

7762
// now, lets add lots of little documents to the collection so we can explore queries and cursors
78-
for (int i=0; i < 100; i++) {
79-
coll.insert(new BasicDBObject().append("i", i));
63+
for (int i = 0; i < 100; i++) {
64+
testCollection.insert(new BasicDBObject().append("i", i));
8065
}
81-
System.out.println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount());
66+
System.out.println("total # of documents after inserting 100 small ones (should be 101) " + testCollection.getCount());
8267

8368
// lets get all the documents in the collection and print them out
84-
DBCursor cursor = coll.find();
69+
DBCursor cursor = testCollection.find();
8570
try {
8671
while (cursor.hasNext()) {
8772
System.out.println(cursor.next());
@@ -91,50 +76,47 @@ public static void main(String[] args) throws Exception {
9176
}
9277

9378
// now use a query to get 1 document out
94-
BasicDBObject query = new BasicDBObject();
95-
query.put("i", 71);
96-
cursor = coll.find(query);
79+
BasicDBObject query = new BasicDBObject("i", 71);
80+
cursor = testCollection.find(query);
9781

9882
try {
99-
while(cursor.hasNext()) {
83+
while (cursor.hasNext()) {
10084
System.out.println(cursor.next());
10185
}
10286
} finally {
10387
cursor.close();
10488
}
10589

10690
// now use a range query to get a larger subset
107-
query = new BasicDBObject();
108-
query.put("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50
109-
cursor = coll.find(query);
91+
query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50
92+
cursor = testCollection.find(query);
11093

11194
try {
112-
while(cursor.hasNext()) {
113-
System.out.println(cursor.next());
114-
}
95+
while (cursor.hasNext()) {
96+
System.out.println(cursor.next());
97+
}
11598
} finally {
11699
cursor.close();
117100
}
118101

119-
// range query with multiple contstraings
120-
query = new BasicDBObject();
121-
query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30
122-
cursor = coll.find(query);
102+
// range query with multiple constraints
103+
query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30
104+
cursor = testCollection.find(query);
123105

124106
try {
125-
while(cursor.hasNext()) {
107+
while (cursor.hasNext()) {
126108
System.out.println(cursor.next());
127109
}
128110
} finally {
129111
cursor.close();
130112
}
131113

132114
// create an index on the "i" field
133-
coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending
115+
testCollection.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending
134116

135117

136118
// list the indexes on the collection
137-
List<DBObject> list = coll.getIndexInfo();
119+
List<DBObject> list = testCollection.getIndexInfo();
138120
for (DBObject o : list) {
139121
System.out.println(o);
140122
}
@@ -154,6 +136,6 @@ public static void main(String[] args) throws Exception {
154136
db.resetError();
155137

156138
// release resources
157-
m.close();
139+
mongoClient.close();
158140
}
159141
}

examples/QuickTourAdmin.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (C) 2008 10gen Inc.
2+
* Copyright (C) 2008-2012 10gen Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,41 +14,40 @@
1414
* limitations under the License.
1515
*/
1616

17-
import com.mongodb.DB;
18-
import com.mongodb.Mongo;
1917
import com.mongodb.BasicDBObject;
18+
import com.mongodb.DB;
19+
import com.mongodb.MongoClient;
2020

2121
public class QuickTourAdmin {
22-
22+
2323
public static void main(String[] args) throws Exception {
2424

2525
// connect to the local database server
26-
Mongo m = new Mongo();
26+
MongoClient mongoClient = new MongoClient();
2727

2828
// Authenticate - optional
2929
// boolean auth = db.authenticate("foo", "bar");
30-
30+
3131
// get db names
32-
for (String s : m.getDatabaseNames()) {
32+
for (String s : mongoClient.getDatabaseNames()) {
3333
System.out.println(s);
3434
}
3535

36-
3736
// get a db
38-
DB db = m.getDB("com_mongodb_MongoAdmin");
37+
DB db = mongoClient.getDB("com_mongodb_MongoAdmin");
3938

4039
// do an insert so that the db will really be created. Calling getDB() doesn't really take any
4140
// action with the server
42-
db.getCollection("testcollection").insert(new BasicDBObject("i",1));
43-
for (String s : m.getDatabaseNames()) {
41+
db.getCollection("testcollection").insert(new BasicDBObject("i", 1));
42+
for (String s : mongoClient.getDatabaseNames()) {
4443
System.out.println(s);
4544
}
4645

4746
// drop a database
48-
m.dropDatabase("com_mongodb_MongoAdmin");
47+
mongoClient.dropDatabase("com_mongodb_MongoAdmin");
4948

50-
for (String s : m.getDatabaseNames()) {
49+
for (String s : mongoClient.getDatabaseNames()) {
5150
System.out.println(s);
52-
}
51+
}
5352
}
5453
}

examples/ReadOplog.java

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// ReadOplog.java
2-
31
/**
4-
* Copyright (C) 2008 10gen Inc.
2+
* Copyright (C) 2008-2012 10gen Inc.
53
*
64
* Licensed under the Apache License, Version 2.0 (the "License");
75
* you may not use this file except in compliance with the License.
@@ -16,47 +14,46 @@
1614
* limitations under the License.
1715
*/
1816

19-
package examples;
20-
21-
import com.mongodb.*;
22-
import org.bson.types.*;
23-
import java.util.*;
17+
import com.mongodb.BasicDBObject;
18+
import com.mongodb.Bytes;
19+
import com.mongodb.DB;
20+
import com.mongodb.DBCollection;
21+
import com.mongodb.DBCursor;
22+
import com.mongodb.DBObject;
23+
import com.mongodb.MongoClient;
24+
import org.bson.types.BSONTimestamp;
2425

2526
public class ReadOplog {
2627

27-
public static void main(String[] args)
28-
throws Exception {
29-
30-
Mongo m = new Mongo();
31-
DB local = m.getDB( "local" );
32-
33-
DBCollection oplog = local.getCollection( "oplog.$main" );
34-
35-
DBObject last = null;
36-
{
37-
DBCursor lastCursor = oplog.find().sort( new BasicDBObject( "$natural" , -1 ) ).limit(1);
38-
if ( ! lastCursor.hasNext() ){
39-
System.out.println( "no oplog!" );
40-
return;
41-
}
42-
last = lastCursor.next();
28+
public static void main(String[] args) throws Exception {
29+
30+
MongoClient mongoClient = new MongoClient();
31+
DB local = mongoClient.getDB("local");
32+
33+
DBCollection oplog = local.getCollection("oplog.$main");
34+
35+
DBCursor lastCursor = oplog.find().sort(new BasicDBObject("$natural", -1)).limit(1);
36+
if (!lastCursor.hasNext()) {
37+
System.out.println("no oplog!");
38+
return;
4339
}
44-
45-
BSONTimestamp ts = (BSONTimestamp)last.get("ts");
46-
System.out.println( "starting point: " + ts );
47-
48-
while ( true ){
49-
System.out.println( "starting at ts: " + ts );
50-
DBCursor cursor = oplog.find( new BasicDBObject( "ts" , new BasicDBObject( "$gt" , ts ) ) );
51-
cursor.addOption( Bytes.QUERYOPTION_TAILABLE );
52-
cursor.addOption( Bytes.QUERYOPTION_AWAITDATA );
53-
while ( cursor.hasNext() ){
40+
DBObject last = lastCursor.next();
41+
42+
BSONTimestamp ts = (BSONTimestamp) last.get("ts");
43+
System.out.println("starting point: " + ts);
44+
45+
while (true) {
46+
System.out.println("starting at ts: " + ts);
47+
DBCursor cursor = oplog.find(new BasicDBObject("ts", new BasicDBObject("$gt", ts)));
48+
cursor.addOption(Bytes.QUERYOPTION_TAILABLE);
49+
cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
50+
while (cursor.hasNext()) {
5451
DBObject x = cursor.next();
55-
ts = (BSONTimestamp)x.get("ts");
56-
System.out.println( "\t" + x );
52+
ts = (BSONTimestamp) x.get("ts");
53+
System.out.println("\t" + x);
5754
}
58-
59-
Thread.sleep( 1000 );
55+
56+
Thread.sleep(1000);
6057
}
6158
}
6259
}

0 commit comments

Comments
 (0)