0% found this document useful (0 votes)
7 views4 pages

Java MongoDB Simple Example

This document contains Java source code for a simple MongoDB example. It imports the MongoDB Java driver dependency, defines some sample data (names and addresses), connects to a MongoDB database locally, and provides a main method that likely inserts the sample data into a MongoDB collection and runs queries. The code is accompanied by brief documentation on MongoDB and Java integration.

Uploaded by

Kishor Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Java MongoDB Simple Example

This document contains Java source code for a simple MongoDB example. It imports the MongoDB Java driver dependency, defines some sample data (names and addresses), connects to a MongoDB database locally, and provides a main method that likely inserts the sample data into a MongoDB collection and runs queries. The code is accompanied by brief documentation on MongoDB and Java integration.

Uploaded by

Kishor Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

<dependency>

<groupId>[Link]</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.10.1</version>
<scope>compile</scope>
</dependency>
( 1 ) Java Project
( 2 ) Java Code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package [Link]!

import [Link]."nkno#n$ost%&ception!
import [Link]&t.'ate(ormat!
import [Link]&t.)arse%&ception!
import [Link]&t.*imple'ate(ormat!
import [Link].+alendar!
import [Link].'ate!
import [Link].$as,ap!
import [Link].-inked-ist!

import [Link]..asic'./bject!
import [Link].'.!
import [Link].'.+ollection!
import [Link].'.+ursor!
import [Link].,ongo+lient!
import [Link].,ongo%&ception!

/00
0 1ava 2 ,ongo'. *imple %&ample
0
0/
public class ,ain 3


static *tring array4names56 7
381on898:im898.rit898;obin898*mit898-ora8981ennifer898-yla898<ic
tor898=dam8>!

static *tring array4address5656 73
38"*89 8(-89 8 ,iami8>9
38"*89 8(-89 8 /rlando8>9
38"*89 8+=89 8*an 'iego8>9
38"*89 8(-89 8 /rlando8>9
38"*89 8(-89 8 /rlando8>9
38"*89 8?@89 8?e# @ork8>9
38"*89 8?@89 8.uffalo8>9
38"*89 8:A89 8 $ouston8>9
38"*89 8+=89 8*an 'iego8>9
38"*89 8:A89 8 $ouston8>
>!

public static void mainB*tring56 argsC 3

try 3

// +onnect to mongodb
,ongo+lient mongo 7 ne# ,ongo+lientB8localost89 2D01DC!

( 3 ) Run
>mvn e&ecEjava
or from eclipse
Source Code i!"u#
Java$ mo%od# a%d !aed &ava$ mo%od#$ mo%od#'&ava'driver$ %os(l o% )
Post navigation
* +%s!alli% ,o%o-. o% /i%do0s 7 ,o%o-. 1 Java (2uer3i%) 4
One thought on MongoDB + Java Simpe !"ampe#
1) Jero
5"a%6s a lo!$ !"is ar!icle 0as ver3 useful for m37
8ree!i%s7
Comme%!s are closed)

$i%e us&'
Object1 Object2
Object3
(oo) *s'
Object4
Share
+echs

You might also like