|
| 1 | ++++ |
| 2 | +date = "2015-03-17T15:36:56Z" |
| 3 | +title = "Admin Quick Tour" |
| 4 | +[menu.main] |
| 5 | + parent = "Getting Started" |
| 6 | + weight = 20 |
| 7 | + pre = "<i class='fa'></i>" |
| 8 | ++++ |
| 9 | + |
| 10 | +# MongoDB Driver Admin Quick Tour |
| 11 | + |
| 12 | +This is the second part of the MongoDB driver quick tour. In the |
| 13 | +[quick tour]({{< relref "getting-started/quick-tour.md" >}}) we looked at how to |
| 14 | +use the Java driver to execute basic CRUD operations. In this section we'll look at some of the |
| 15 | +administrative features available in the driver. |
| 16 | + |
| 17 | +The following code snippets come from the `QuickTourAdmin.java` example code |
| 18 | +that can be found with the [driver |
| 19 | +source]({{< srcref "src/examples/example/QuickTourAdmin.java">}}). |
| 20 | + |
| 21 | +{{% note %}} |
| 22 | +See the [installation guide]({{< relref "getting-started/installation-guide.md" >}}) |
| 23 | +for instructions on how to install the MongoDB Driver. |
| 24 | +{{% /note %}} |
| 25 | + |
| 26 | +## Setup |
| 27 | + |
| 28 | +To get use started we'll quickly connect and create a `mongoClient`, `database` and `collection` |
| 29 | +variable for use in the examples below: |
| 30 | + |
| 31 | +```java |
| 32 | +MongoClient mongoClient = new MongoClient(); |
| 33 | +MongoDatabase database = mongoClient.getDatabase("mydb"); |
| 34 | +MongoCollection<Document> collection = database.getCollection("test"); |
| 35 | +``` |
| 36 | + |
| 37 | +## Getting A List of Databases |
| 38 | + |
| 39 | +You can get a list of the available databases: |
| 40 | + |
| 41 | +```java |
| 42 | +MongoClient mongoClient = new MongoClient(); |
| 43 | + |
| 44 | +for (String s : mongoClient.getDatabaseNames()) { |
| 45 | + System.out.println(s); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Calling `mongoClient.getDB()` does not create a database. Only when a |
| 50 | +database is written to will a database be created. Examples would be |
| 51 | +creating an index or collection or inserting a document. |
| 52 | + |
| 53 | +## Dropping A Database |
| 54 | + |
| 55 | +You can drop a database by name using a `MongoClient` instance: |
| 56 | + |
| 57 | +```java |
| 58 | +MongoClient mongoClient = new MongoClient(); |
| 59 | +mongoClient.dropDatabase("databaseToBeDropped"); |
| 60 | +``` |
| 61 | + |
| 62 | +## Creating A Collection |
| 63 | + |
| 64 | +There are two ways to create a collection. Inserting a document will |
| 65 | +create the collection if it doesn't exist or calling the |
| 66 | +[createCollection](http://docs.mongodb.org/manual/reference/method/db.createCollection) |
| 67 | +command. |
| 68 | + |
| 69 | +An example of creating a capped sized to 1 megabyte: |
| 70 | + |
| 71 | +```java |
| 72 | +db = mongoClient.getDB("mydb"); |
| 73 | +db.createCollection("testCollection", new BasicDBObject("capped", true) |
| 74 | + .append("size", 1048576)); |
| 75 | +``` |
| 76 | + |
| 77 | +## Getting A List of Collections |
| 78 | + |
| 79 | +You can get a list of the available collections in a database: |
| 80 | + |
| 81 | +```java |
| 82 | +for (String s : db.getCollectionNames()) { |
| 83 | + System.out.println(s); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +It should output |
| 88 | + |
| 89 | +```ini |
| 90 | +system.indexes |
| 91 | +testCollection |
| 92 | +``` |
| 93 | + |
| 94 | +{{% note %}} |
| 95 | +The system.indexes collection is automatically created and lists all |
| 96 | +the indexes in the database and shouldn't be accessed directly. |
| 97 | +{{% /note %}} |
| 98 | + |
| 99 | +## Dropping A Collection |
| 100 | + |
| 101 | +You can drop a collection by using the drop() method: |
| 102 | + |
| 103 | +```java |
| 104 | +DBCollection coll = db.getCollection("testCollection"); |
| 105 | +coll.drop(); |
| 106 | +System.out.println(db.getCollectionNames()); |
| 107 | +``` |
| 108 | + |
| 109 | +And you should notice that testCollection has been dropped. |
| 110 | + |
| 111 | +## Getting a List of Indexes on a Collection |
| 112 | + |
| 113 | +You can get a list of the indexes on a collection: |
| 114 | + |
| 115 | +```java |
| 116 | +List<DBObject> list = coll.getIndexInfo(); |
| 117 | + |
| 118 | +for (DBObject o : list) { |
| 119 | + System.out.println(o.get("key")); |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +and you should see something like |
| 124 | + |
| 125 | +```json |
| 126 | +{ "v" : 1 , "key" : { "_id" : 1} , "name" : "_id_" , "ns" : "mydb.testCollection"} |
| 127 | +{ "v" : 1 , "key" : { "i" : 1} , "name" : "i_1" , "ns" : "mydb.testCollection"} |
| 128 | +{ "v" : 1 , "key" : { "loc" : "2dsphere"} , "name" : "loc_2dsphere" , ... } |
| 129 | +{ "v" : 1 , "key" : { "_fts" : "text" , "_ftsx" : 1} , "name" : "content_text" , ... } |
| 130 | +``` |
| 131 | + |
| 132 | +## Creating An Index |
| 133 | + |
| 134 | +MongoDB supports indexes, and they are very easy to add on a collection. |
| 135 | +To create an index, you just specify the field that should be indexed, |
| 136 | +and specify if you want the index to be ascending (`1`) or descending |
| 137 | +(`-1`). The following creates an ascending index on the `i` field : |
| 138 | + |
| 139 | +```java |
| 140 | +coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending |
| 141 | +``` |
| 142 | + |
| 143 | +## Geo indexes |
| 144 | + |
| 145 | +MongoDB supports various [geospatial indexes]({{< docref "core/geospatial-indexes/" >}}) |
| 146 | +in this example we'll be creating a 2dsphere index which we can query using standard |
| 147 | +GeoJson markup. To create a 2dsphere index specify the string literal |
| 148 | +"2dsphere" in the index document: |
| 149 | + |
| 150 | +```java |
| 151 | +coll.createIndex(new BasicDBObject("loc", "2dsphere")); |
| 152 | +``` |
| 153 | + |
| 154 | +There are various ways to |
| 155 | +query a [2dsphere index]({{< docref "/tutorial/query-a-2dsphere-index">}} this example |
| 156 | +finds places within 500 meters of a location: |
| 157 | + |
| 158 | +```java |
| 159 | +BasicDBList coordinates = new BasicDBList(); |
| 160 | +coordinates.put(0, -73.97); |
| 161 | +coordinates.put(1, 40.77); |
| 162 | +coll.insert(new BasicDBObject("name", "Central Park") |
| 163 | + .append("loc", new BasicDBObject("type", "Point").append("coordinates", coordinates)) |
| 164 | + .append("category", "Parks")); |
| 165 | + |
| 166 | +coordinates.put(0, -73.88); |
| 167 | +coordinates.put(1, 40.78); |
| 168 | +coll.insert(new BasicDBObject("name", "La Guardia Airport") |
| 169 | + .append("loc", new BasicDBObject("type", "Point").append("coordinates", coordinates)) |
| 170 | + .append("category", "Airport")); |
| 171 | + |
| 172 | + |
| 173 | +// Find whats within 500m of my location |
| 174 | +BasicDBList myLocation = new BasicDBList(); |
| 175 | +myLocation.put(0, -73.965); |
| 176 | +myLocation.put(1, 40.769); |
| 177 | +myDoc = coll.findOne( |
| 178 | + new BasicDBObject("loc", |
| 179 | + new BasicDBObject("$near", |
| 180 | + new BasicDBObject("$geometry", |
| 181 | + new BasicDBObject("type", "Point") |
| 182 | + .append("coordinates", myLocation)) |
| 183 | + .append("$maxDistance", 500) |
| 184 | + ) |
| 185 | + ) |
| 186 | + ); |
| 187 | +System.out.println(myDoc.get("name")); |
| 188 | +``` |
| 189 | + |
| 190 | +It should print *Central Park*. See the |
| 191 | +[geospatial documentation]({{< docref "/reference/operator/query-geospatial">}}) for |
| 192 | +more information. |
| 193 | + |
| 194 | +## Text indexes |
| 195 | + |
| 196 | +MongoDB also provides text indexes to support text search of string |
| 197 | +content. Text indexes can include any field whose value is a string or |
| 198 | +an array of string elements. To create a text index specify the string |
| 199 | +literal "text" in the index document: |
| 200 | + |
| 201 | +```java |
| 202 | +// create a text index on the "content" field |
| 203 | +coll.createIndex(new BasicDBObject("content", "text")); |
| 204 | +``` |
| 205 | + |
| 206 | +As of MongoDB 2.6 text indexes are now integrated into the main query |
| 207 | +language and enabled by default: |
| 208 | + |
| 209 | +```java |
| 210 | +// Insert some documents |
| 211 | +coll.insert(new BasicDBObject("_id", 0).append("content", "textual content")); |
| 212 | +coll.insert(new BasicDBObject("_id", 1).append("content", "additional content")); |
| 213 | +coll.insert(new BasicDBObject("_id", 2).append("content", "irrelevant content")); |
| 214 | + |
| 215 | +// Find using the text index |
| 216 | +BasicDBObject search = new BasicDBObject("$search", "textual content -irrelevant"); |
| 217 | +BasicDBObject textSearch = new BasicDBObject("$text", search); |
| 218 | +int matchCount = coll.find(textSearch).count(); |
| 219 | +System.out.println("Text search matches: "+ matchCount); |
| 220 | + |
| 221 | +// Find using the $language operator |
| 222 | +textSearch = new BasicDBObject("$text", search.append("$language", "english")); |
| 223 | +matchCount = coll.find(textSearch).count(); |
| 224 | +System.out.println("Text search matches (english): "+ matchCount); |
| 225 | + |
| 226 | +// Find the highest scoring match |
| 227 | +BasicDBObject projection = new BasicDBObject("score", new BasicDBObject("$meta", "textScore")); |
| 228 | +myDoc = coll.findOne(textSearch, projection); |
| 229 | +System.out.println("Highest scoring document: "+ myDoc); |
| 230 | +``` |
| 231 | + |
| 232 | +and it should print: |
| 233 | + |
| 234 | +```ini |
| 235 | +Text search matches: 2 |
| 236 | +Text search matches (english): 2 |
| 237 | +Highest scoring document: { "_id" : 1 , "content" : "additional content" , "score" : 0.75} |
| 238 | +``` |
| 239 | + |
| 240 | +For more information about text search see the [text index]({{< docsref "/core/index-text" >}}) and |
| 241 | +[$text query operator]({{< docsref "/reference/operator/query/text">}}) documentation. |
0 commit comments