Main Tutorials

Java MongoDB Tutorial

mongodb-tutorials

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 :

  1. MongoDB 2.2.3
  2. Java-MongoDB-Driver 2.11.0
  3. Spring-Data-MongoDB 1.2.0.RELEASE

1. MongoDB Core Examples

MongoDB installation, configuration, connect, query and backup.

2. Java MongoDB Examples

Java MongoDB driver APIs examples to perform insert, update, query and delete documents from MongoDB.

3. Spring Data MongoDB Examples

Spring Data for MongoDB examples to perform insert, update, query and delete documents from MongoDB.

4. MongoDB FAQs

Some common questions and answers in MongoDB.

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
15 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Dhyanandra Singh
8 years ago

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

Snehal
6 years ago

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 ?

Satyajit Nayak
5 years ago

how to pass host, port,dbname,password dynamicaly in react mongodb in spring boot react..

Me
6 years ago

It was very helpful!
Thanks!! 🙂

Carol
6 years ago

Thanks for this complete tutorial

Irfan
7 years ago

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

Rathinavel
7 years ago

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.

Sameer Bhatia
7 years ago

Can you please share an example with MondoDB and Hibernate ?

rew
7 years ago

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}]}

hari
9 years ago

Hello please help me to learn java play framework from scratch

Alex
9 years ago

“Auto Sequence ID Example” links to “Hello World example” and vice versa..

tank
9 years ago

Spring Data MongoDB hello world example wrong link

mrugesh
10 years ago

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..

Om Prakash Shakya
10 years ago

Excellent stuffs regarding mongodb. Try mongodb commands online http://www.domongodb.com

nirmal
10 years ago