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

15 comments on “Java MongoDB Tutorial

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

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

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

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

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

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

Leave a Comment

Your email address will not be published. Required fields are marked *