|
| 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 "driver/src/examples/tour/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 | + |
| 38 | +## Get A List of Databases |
| 39 | + |
| 40 | +You can get a list of the available databases: |
| 41 | + |
| 42 | +```java |
| 43 | +for (String name: mongoClient.listDatabaseNames()) { |
| 44 | + System.out.println(name); |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Calling the `getDatabase()` on `MongoClient` does not create a database. |
| 49 | +Only when a database is written to will a database be created. Examples |
| 50 | +would be creating an index or collection or inserting a document into a |
| 51 | +collection. |
| 52 | + |
| 53 | +## Drop A Database |
| 54 | + |
| 55 | +You can drop a database by name using a `MongoClient` instance: |
| 56 | + |
| 57 | +```java |
| 58 | +mongoClient.dropDatabase("databaseToBeDropped"); |
| 59 | +``` |
| 60 | + |
| 61 | +## Create A Collection |
| 62 | + |
| 63 | +Collections in MongoDB are created automatically simply by inserted a document into it. Using the `[createCollection]({{< apiref "com/mongodb/client/MongoDatabase.html#createCollection-java.lang.String-">}})` method, you can also create a collection explicitly in order to to customize its configuration. For example, to create a capped collection sized to 1 megabyte: |
| 64 | + |
| 65 | +```java |
| 66 | +database.createCollection("cappedCollection", new |
| 67 | +CreateCollectionOptions().capped(true).sizeInBytes(0x100000)); |
| 68 | +``` |
| 69 | + |
| 70 | +## Get A List of Collections |
| 71 | + |
| 72 | +You can get a list of the available collections in a database: |
| 73 | + |
| 74 | +```java |
| 75 | +for (String name : database.listCollectionNames()) { |
| 76 | + System.out.println(name); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## Drop A Collection |
| 81 | + |
| 82 | +You can drop a collection by using the drop() method: |
| 83 | + |
| 84 | +```java |
| 85 | +collection.dropCollection(); |
| 86 | +``` |
| 87 | + |
| 88 | +## Create An Index |
| 89 | + |
| 90 | +MongoDB supports secondary indexes. To create an index, you just |
| 91 | +specify the field or combination of fields, and for each field specify the direction of the index for that field. |
| 92 | +For `1` ascending or `-1` for descending. The following creates an ascending index on the `i` field: |
| 93 | + |
| 94 | +```java |
| 95 | +// create an ascending index on the "i" field |
| 96 | + collection.createIndex(new Document("i", 1)); |
| 97 | +``` |
| 98 | + |
| 99 | +## Get a List of Indexes on a Collection |
| 100 | + |
| 101 | +Use the `listIndexes()` method to get a list of indexes on a collection: |
| 102 | +The following lists the indexes on the collection `test` |
| 103 | + |
| 104 | +```java |
| 105 | +for (final Document index : collection.listIndexes()) { |
| 106 | + System.out.println(index.toJson()); |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +The example should print the following indexes: |
| 111 | + |
| 112 | +```json |
| 113 | +{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.test" } |
| 114 | +{ "v" : 1, "key" : { "i" : 1 }, "name" : "i_1", "ns" : "mydb.test" } |
| 115 | +``` |
| 116 | + |
| 117 | +## Text indexes |
| 118 | + |
| 119 | +MongoDB also provides text indexes to support text search of string |
| 120 | +content. Text indexes can include any field whose value is a string or |
| 121 | +an array of string elements. To create a text index specify the string |
| 122 | +literal "text" in the index document: |
| 123 | + |
| 124 | +```java |
| 125 | +// create a text index on the "content" field |
| 126 | +coll.createIndex(new Document("content", "text")); |
| 127 | +``` |
| 128 | + |
| 129 | +As of MongoDB 2.6, text indexes are now integrated into the main query |
| 130 | +language and enabled by default (here we use the [`Filters.text`]({{< apiref "com/mongodb/client/model/Filters.html#text-java.lang.String-">}}) helper): |
| 131 | + |
| 132 | +```java |
| 133 | +// Insert some documents |
| 134 | +collection.insertOne(new Document("_id", 0).append("content", "textual content")); |
| 135 | +collection.insertOne(new Document("_id", 1).append("content", "additional content")); |
| 136 | +collection.insertOne(new Document("_id", 2).append("content", "irrelevant content")); |
| 137 | + |
| 138 | +// Find using the text index |
| 139 | +long matchCount = collection.count(text("textual content -irrelevant")); |
| 140 | +System.out.println("Text search matches: " + matchCount); |
| 141 | + |
| 142 | +// Find using the $language operator |
| 143 | +Bson textSearch = text("textual content -irrelevant", "english"); |
| 144 | +matchCount = collection.count(textSearch); |
| 145 | +System.out.println("Text search matches (english): " + matchCount); |
| 146 | + |
| 147 | +// Find the highest scoring match |
| 148 | +Document projection = new Document("score", new Document("$meta", "textScore")); |
| 149 | +Document myDoc = collection.find(textSearch).projection(projection).first(); |
| 150 | +System.out.println("Highest scoring document: " + myDoc.toJson()); |
| 151 | +``` |
| 152 | + |
| 153 | +and it should print: |
| 154 | + |
| 155 | +```json |
| 156 | +Text search matches: 2 |
| 157 | +Text search matches (english): 2 |
| 158 | +Highest scoring document: { "_id" : 1, "content" : "additional content", "score" : 0.75 } |
| 159 | +``` |
| 160 | + |
| 161 | +For more information about text search see the [text index]({{< docsref "/core/index-text" >}}) and |
| 162 | +[$text query operator]({{< docsref "/reference/operator/query/text">}}) documentation. |
| 163 | + |
| 164 | +That concludes the admin quick tour overview! Remember any [command]({{< docsref "/reference/command">}}) that doesn't have a specific helper can be called by the [database.runCommand()](http://api.mongodb.org/java/3.0/?com/mongodb/async/client/MongoDatabase.html#runCommand-org.bson.conversions.Bson-com.mongodb.ReadPreference-com.mongodb.async.SingleResultCallback-). |
0 commit comments