1
1
/**
2
- * Copyright (C) 2008 10gen Inc.
2
+ * Copyright (C) 2008-2012 10gen Inc.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
14
14
* limitations under the License.
15
15
*/
16
16
17
- import com .mongodb .Mongo ;
18
- import com .mongodb .DBCollection ;
19
17
import com .mongodb .BasicDBObject ;
20
- import com .mongodb .DBObject ;
21
- import com .mongodb .DBCursor ;
22
18
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 ;
24
23
25
- import java .util .Set ;
26
24
import java .util .List ;
25
+ import java .util .Set ;
27
26
28
27
public class QuickTour {
29
28
30
29
public static void main (String [] args ) throws Exception {
31
30
32
31
// connect to the local database server
33
- Mongo m = new Mongo ();
34
-
35
- m .setWriteConcern (WriteConcern .SAFE );
32
+ MongoClient mongoClient = new MongoClient ();
36
33
37
34
// get handle to "mydb"
38
- DB db = m .getDB ( "mydb" );
39
-
35
+ DB db = mongoClient .getDB ("mydb" );
36
+
40
37
// Authenticate - optional
41
38
// boolean auth = db.authenticate("foo", "bar");
42
39
43
-
44
40
// 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 ) {
47
43
System .out .println (s );
48
44
}
49
45
50
46
// 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" );
55
48
49
+ // drop all the data in it
50
+ testCollection .drop ();
56
51
57
52
// 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 ));
65
55
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 );
72
57
73
58
// 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 ();
75
60
System .out .println (myDoc );
76
61
77
62
// 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 ));
80
65
}
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 ());
82
67
83
68
// lets get all the documents in the collection and print them out
84
- DBCursor cursor = coll .find ();
69
+ DBCursor cursor = testCollection .find ();
85
70
try {
86
71
while (cursor .hasNext ()) {
87
72
System .out .println (cursor .next ());
@@ -91,50 +76,47 @@ public static void main(String[] args) throws Exception {
91
76
}
92
77
93
78
// 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 );
97
81
98
82
try {
99
- while (cursor .hasNext ()) {
83
+ while (cursor .hasNext ()) {
100
84
System .out .println (cursor .next ());
101
85
}
102
86
} finally {
103
87
cursor .close ();
104
88
}
105
89
106
90
// 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 );
110
93
111
94
try {
112
- while (cursor .hasNext ()) {
113
- System .out .println (cursor .next ());
114
- }
95
+ while (cursor .hasNext ()) {
96
+ System .out .println (cursor .next ());
97
+ }
115
98
} finally {
116
99
cursor .close ();
117
100
}
118
101
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 );
123
105
124
106
try {
125
- while (cursor .hasNext ()) {
107
+ while (cursor .hasNext ()) {
126
108
System .out .println (cursor .next ());
127
109
}
128
110
} finally {
129
111
cursor .close ();
130
112
}
131
113
132
114
// 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
134
116
135
117
136
118
// list the indexes on the collection
137
- List <DBObject > list = coll .getIndexInfo ();
119
+ List <DBObject > list = testCollection .getIndexInfo ();
138
120
for (DBObject o : list ) {
139
121
System .out .println (o );
140
122
}
@@ -154,6 +136,6 @@ public static void main(String[] args) throws Exception {
154
136
db .resetError ();
155
137
156
138
// release resources
157
- m .close ();
139
+ mongoClient .close ();
158
140
}
159
141
}
0 commit comments