Java MongoDB Tutorial
MongoDB, noSQL open source database, written in C++, with many great features like map-reduce , auto sharding, replication, high availability and etc.
The following Java / Spring Data MongoDB tutorials and examples are tested with :
- MongoDB 2.2.3
- Java-MongoDB-Driver 2.11.0
- Spring-Data-MongoDB 1.2.0.RELEASE
1. MongoDB Core Examples
MongoDB installation, configuration, connect, query and backup.
-
Install MongoDB on Windows
How to install MongoDB on Windows. -
Install MongoDB on Ubuntu
How to install MongoDB on Ubuntu. -
Install MongoDB on Mac OS X
How to install MongoDB on Mac OS X. -
MongoDB Hello World Example
How to do CRUD and indexing in MongoDB. -
MongoDB authentication example
Start MongoDB in secure mode, authentication is required. -
MongoDB import and export example
Backup with mongoexport, restore with mongoimport in MongoDB.
2. Java MongoDB Examples
Java MongoDB driver APIs examples to perform insert, update, query and delete documents from MongoDB.
-
Java MongoDB hello world example
Classic hello world example to show you how to use Java MongoDB driver APIs to perform CRUD in MongoDB. -
Java MongoDB : Authentication Example
Example to perform authentication access to MongoDB. -
Java MongoDB : Insert a document
4 ways to insert JSON data into MongoDB. -
Java MongoDB : Update document
Example to use collection.update() to update an existing document -
Java MongoDB : Query document
Example to use collection.find() to get / query document from collection. -
Java MongoDB : Delete document
Example to use collection.remove() to delete documents from collection. -
Java MongoDB : Save image example
Use GridFS APIs to save binary file into MongoDB.
3. Spring Data MongoDB Examples
Spring Data for MongoDB examples to perform insert, update, query and delete documents from MongoDB.
-
Spring Data MongoDB hello world example
Configure (both XML and annotation) and perform CRUD operations with “Spring Data for MongoDB” framework. -
Spring Data MongoDB : Insert document
Example to use Spring data save() and insert() to save domain object into MongoDB database. -
Spring Data MongoDB : Update document
Example to use Spring data save(), updateFirst() and updateMulti() to update existing domain object from MongoDB database. -
Spring Data MongoDB : Query document
Example to use Spring data findOne(), find() and getCollection() to get / query documents from MongoDB. -
Spring Data MongoDB : Delete document
Example to remove() and findAndRemove() to delete documents from MongoDB. -
Spring Data MongoDB : Save binary file, GridFS example
Use GridFS in Spring Data MongoDB, to save the binary file in MongoDB. -
Spring Data MongoDB – Auto Sequence ID Example
How to create an auto increase sequence id.
4. MongoDB FAQs
Some common questions and answers in MongoDB.
- Repair a crashed MongoDB server
- Couldn’t Open /Data/Db/Yourdb.Ns Errno:13 Permission Denied
- Java MongoDB : Convert JSON data to DBObject
- Java MongoDB : Get collection from database
- Spring Data MongoDB Remove _class Column
hello Mkyong, currently i am using Embedded document design in mongodb. and i want to use it with spring data how can i do that
I am working on java application using mongo, I want to check if there is any other option to remove documents from collection after some time period… I checked createIndex option for implementing TTL .. but it needs timestamp column to be present in collection, is there any other way to implement TTL with timestamp column and createIndex option ?
how to pass host, port,dbname,password dynamicaly in react mongodb in spring boot react..
It was very helpful!
Thanks!! 🙂
Thanks for this complete tutorial
Hello Mkyong!
I want to build a search panel, in which I will put name and it will show me all data from database related to that name.
I want to create that in java with database Mongodb
Hello Mkyong,
Is there any way to use multi update in same array object using java.
{
“_id” : ObjectId(“58859d758c0f1c0c385a2f42”),
“medications” : [
{
“id” : 23.0,
“name” : “A”,
“type” : “B”
},
{
“id” : 41.0,
“name” : “C”,
“type” : “D”
},
{
“id” : 41.0,
“name” : “C”,
“type” : “D”
}
]
}
db.tutorial.update({ “medications.id”: 41.0 },{“$set”:{“medications.$.name”:”Test”}},{“multi”:true})
above query is updating only one element in array
please help me soon.
Can you please share an example with MondoDB and Hibernate ?
I have this query in JS
arrStrudent = db.students.aggregate([{$project:{scores: {$filter: {input: “$scores”,as: “item”,cond: { $eq: [“$$item.type”, “homework”]}}}}},{“$unwind”: “$scores”},{ $sort : { _id : 1, scores: 1 } },{$project : { “scores.score” :1} }]);null;
var tmpId = -1;
while (arrStrudent.hasNext()) {v = arrStrudent.next();if (v._id != tmpId) {print(v._id + “==>” + v.scores.score);tmpId = v._id;db.students.update({ _id : v._id},{ $pull: { scores: { score : v.scores.score }}})}}
arrStrudent.close();
this is to remove lowest score within the scores array of student collections.
===============
Now I want do same in Java, can you help me with aggregate query ? I worte something like this, and this doesnt work.
AggregateIterable iterable = db.getCollection(“students”).aggregate(asList(
new Document(“$project”, new Document(“scores”, new Document(“$filter”, new Document(“input”,”$scores”).append(“as”, “item”).append(“cond”, new Document(“$eq”,new Document(“$$item.type”, “homework”))))))
));
=================
student collection looks like this.
{“_id” : 19,”name” : “Gisela Levin”,”scores” : [{“type” : “exam”,”score” : 44.51211101958831},{“type” : “quiz”,”score” : 0.6578497966368002},{“type” : “homework”,”score” : 93.36341655949683}]}
Hello please help me to learn java play framework from scratch
“Auto Sequence ID Example” links to “Hello World example” and vice versa..
Spring Data MongoDB hello world example wrong link
hello, right now i am doing research on mongodb’s auto sharding
proces..i can run simple connection of mongodb through java code..but i
cant find java code for sharding process..so can u please send me that code..
thanks for adavance..
Excellent stuffs regarding mongodb. Try mongodb commands online http://www.domongodb.com
Some more examples
http://www.pretechsol.com/p/nosql_20.html#.UeZpTRnZhaE