Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mongodb
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gitlab
mongodb
Commits
4e2c0443
Unverified
Commit
4e2c0443
authored
Sep 12, 2017
by
solidnerd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update app
parent
8e34c2cc
Pipeline
#703
failed with stages
in 1 minute and 11 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
24 deletions
+19
-24
App.java
src/main/java/com/bee42/mongo/App.java
+19
-24
No files found.
src/main/java/com/bee42/mongo/App.java
View file @
4e2c0443
package
com
.
bee42
.
mongo
;
import
java.net.UnknownHostException
;
import
java.util.Date
;
import
com.mongodb.BasicDBObject
;
import
com.mongodb.DB
;
import
com.mongodb.DBCollection
;
import
com.mongodb.DBCursor
;
import
com.mongodb.MongoClient
;
import
com.mongodb.MongoException
;
import
com.mongodb.*
;
import
com.mongodb.client.MongoCollection
;
import
com.mongodb.client.MongoCursor
;
import
com.mongodb.client.MongoDatabase
;
import
static
com
.
mongodb
.
client
.
model
.
Filters
.
eq
;
/**
* Java + MongoDB Hello world Example
...
...
@@ -19,30 +20,26 @@ public class App {
try
{
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClientURI
connectionString
=
new
MongoClientURI
(
"mongodb://localhost:27017"
);
MongoClient
mongo
=
new
MongoClient
(
"localhost"
,
27017
);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB
db
=
mongo
.
getDB
(
"testdb"
);
MongoDatabase
db
=
mongo
.
getDatabase
(
"testdb"
);
/**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DB
Collection
table
=
db
.
getCollection
(
"user"
);
Mongo
Collection
table
=
db
.
getCollection
(
"user"
);
/**** Insert ****/
// create a document to store key and value
BasicDBObject
document
=
new
BasicDBObject
();
document
.
put
(
"name"
,
"
mkyong
"
);
document
.
put
(
"age"
,
30
);
document
.
put
(
"name"
,
"
bee
"
);
document
.
put
(
"age"
,
"20102"
);
document
.
put
(
"createdDate"
,
new
Date
());
table
.
insert
(
document
);
table
.
insert
One
(
document
);
/**** Find and display ****/
BasicDBObject
searchQuery
=
new
BasicDBObject
();
searchQuery
.
put
(
"name"
,
"mkyong"
);
DBCursor
cursor
=
table
.
find
(
searchQuery
);
MongoCursor
cursor
=
table
.
find
().
filter
(
eq
(
"name"
,
"bee"
)).
iterator
();
while
(
cursor
.
hasNext
())
{
System
.
out
.
println
(
cursor
.
next
());
...
...
@@ -51,21 +48,21 @@ public class App {
/**** Update ****/
// search document where name="mkyong" and update it with new values
BasicDBObject
query
=
new
BasicDBObject
();
query
.
put
(
"name"
,
"
mkyong
"
);
query
.
put
(
"name"
,
"
bee
"
);
BasicDBObject
newDocument
=
new
BasicDBObject
();
newDocument
.
put
(
"name"
,
"
mkyong
-updated"
);
newDocument
.
put
(
"name"
,
"
bee
-updated"
);
BasicDBObject
updateObj
=
new
BasicDBObject
();
updateObj
.
put
(
"$set"
,
newDocument
);
table
.
update
(
query
,
updateObj
);
table
.
update
One
(
query
,
updateObj
);
/**** Find and display ****/
BasicDBObject
searchQuery2
=
new
BasicDBObject
().
append
(
"name"
,
"mkyong-updated"
);
DBCursor
cursor2
=
table
.
find
(
searchQuery2
);
MongoCursor
cursor2
=
table
.
find
().
filter
(
eq
(
"name"
,
"bee-updated"
)).
iterator
(
);
while
(
cursor2
.
hasNext
())
{
System
.
out
.
println
(
cursor2
.
next
());
...
...
@@ -74,8 +71,6 @@ public class App {
/**** Done ****/
System
.
out
.
println
(
"Done"
);
}
catch
(
UnknownHostException
e
)
{
e
.
printStackTrace
();
}
catch
(
MongoException
e
)
{
e
.
printStackTrace
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment