Skip to content

Commit 76be4c7

Browse files
committed
Ported 2.13 docs
JAVA-1733
1 parent ac593ef commit 76be4c7

File tree

182 files changed

+41760
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+41760
-0
lines changed

docs/reference/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# MongoDB Java Driver Documentation
2+
3+
## Quick Start
4+
5+
1. Install hugo
6+
2. Create `./data/mongodb.toml`
7+
3. Create content
8+
9+
## Requirements
10+
Hugo version 0.13 [download here](https://github.com/spf13/hugo/releases/tag/v0.13)
11+
<br>
12+
Check out the hugo [quickstart guide](http://gohugo.io/overview/quickstart/).
13+
14+
## Running the server
15+
To run the server call the hugo command:
16+
17+
hugo server --baseUrl=http://localhost/ --buildDrafts --watch
18+
19+
| Options explained ||
20+
| --------------------------- |--------------------------------------------------------------------------|
21+
| server | Hugo runs its own webserver to render the files |
22+
| --baseUrl=http://localhost/ | Normally the base url will be /mongo-java-driver for gh-pages |
23+
| --buildDrafts | Include draft posts in the output - these won't be published to gh-pages |
24+
| -- watch | Automatically reloads on file change |
25+
26+
27+
All generated content will appear in the `./public` folder, so you can also check the filesystem and browse it locally.<br>
28+
For more hugo server options run: `hugo --help`
29+
30+
### Data
31+
32+
You *should* create `./data/mongodb.toml` and copy the `themes/mongodb/data/mongodb.toml` file and set the values as needed.
33+
34+
## Creating new content
35+
36+
Handy [markdown cheat sheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Here-Cheatsheet) will help with markdown issues and in markdown you can fall back to html if needed.
37+
38+
To create new content run: `hugo new <contentFileName>.md` and the new file will created in the `./content` directory and marked as a `draft`.
39+
40+
Its a good idea to group similar content together by placing it in a directory which can also be done by the `new` command eg: `hugo new tutorial/newTutorial.md`
41+
42+
### Menus
43+
Menu configuration generally goes in the content
44+
[front matter](http://gohugo.io/content/front-matter/). See the hugo [menu](http://gohugo.io/extras/menus/) docs, menus can also be configured in the top level `config.toml`.
45+
46+
The weight parameter relates to where it will appear in the menu starting with the lowest at the top and the highest at the bottom (heavy things sink).
47+
48+
## MongoDB Theme
49+
**Do not delete / change anything in themes**.<br>
50+
Currently, `./themes/mongodb` provides all templates and configuration. You can override *anything* by providing a top level version in the top level directory (`data, layouts, static`), so feel free to copy or add your own layouts.
51+
52+
You shouldn't edit the mongodb theme directly this will eventually be removed and live in its own repo for ease of sharing.

docs/reference/config.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
baseurl = "/mongo-java-driver/2.13"
2+
languageCode = "en-us"
3+
title = "MongoDB Java Driver"
4+
theme = "mongodb"
5+
canonifyurls = false
6+
7+
# Example custom menu Item (Useful for linking to API docs)
8+
# [[menu.main]]
9+
# name = "Issues & Help"
10+
# pre = "<i class='fa fa-life-ring'></i>"
11+
# weight = 100
12+
# identifier = "help"
13+
# url = "https://jira.mongodb.org/browse/JAVA"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
+++
2+
date = "2015-03-17T15:36:56Z"
3+
title = "Getting Started"
4+
[menu.main]
5+
weight = 20
6+
pre = "<i class='fa fa-road'></i>"
7+
+++
8+
9+
## Getting Started
10+
11+
To help you get started quickly on the new driver, follow:
12+
13+
* [Installation]({{< ref "getting-started/installation-guide.md" >}})
14+
* [Quick Tour]({{< ref "getting-started/quick-tour.md" >}})
15+
* [Admin Quick Tour]({{< ref "getting-started/quick-tour-admin.md" >}})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
+++
2+
date = "2015-03-17T15:36:56Z"
3+
title = "Installation Guide"
4+
[menu.main]
5+
parent = "Getting Started"
6+
weight = 1
7+
pre = "<i class='fa'></i>"
8+
+++
9+
10+
# Installation
11+
12+
The recommended way to get started using one of the drivers in your project is with a dependency management system.
13+
14+
{{< distroPicker >}}
15+
16+
## MongoDB Java Driver
17+
18+
This jar that contains everything you need including the BSON library.
19+
20+
{{< install artifactId="mongo-java-driver" version="2.13.0" >}}
21+
22+
## BSON
23+
24+
This library comprehensively supports [BSON](http://www.bsonspec.org),
25+
the data storage and network transfer format that MongoDB uses for "documents".
26+
BSON is short for Binary [JSON](http://json.org/), is a binary-encoded serialization of JSON-like documents.
27+
28+
{{< install artifactId="bson" version="2.13.0" >}}
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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

Comments
 (0)