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.
1414 * limitations under the License.
1515 */
1616
17- import com .mongodb .Mongo ;
18- import com .mongodb .DBCollection ;
1917import com .mongodb .BasicDBObject ;
20- import com .mongodb .DBObject ;
21- import com .mongodb .DBCursor ;
2218import 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 ;
2624import java .util .List ;
25+ import java .util .Set ;
2726
2827public 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}
0 commit comments