Search code examples
javadatabasemongodbbson

Confused about MongoDB and Pooling, also Bson syntax and Document formats


I've only just started using MongoDB (2 hours in) and I'm super confused about a few things.

Firstly, I read that MongoDB connection pooling isn't needed as it already does that for you. Is this true?

Secondly, I am creating profiles in a collection, the some of the profiles may have information like expiry dates, and passwords, some do not. Is it possible to just set their document to contain these and still be a member of the same collection?

Thirdly, I'm using the UUID for each document, would this be the way to retrieve a specific document:

public static Document getPlayer(String uuid) {
    return players.find(Document.parse("{uuid : " + uuid + "}")).first();
}

Is the syntax correct? I currently don't have the ability to run the code as I do not have a MongoDB server.

Thank you for your help.. I am currently reading through the docs and learning more and more.


Solution

  • Firstly, I read that MongoDB connection pooling isn't needed as it already does that for you. Is this true?

    Whenever you create a mongoClient is has a connection pool associated with it. You're essentially not required to handle these connections yourself but use a single client object throughout your application. You may want to take a look at connection document here.

    Secondly, I am creating profiles in a collection, the some of the profiles may have information like expiry dates, and passwords, some do not. Is it possible to just set their document to contain these and still be a member of the same collection?

    This is absolutely possible in MongoDB. It allows you to store polymorphic data in a single collection.

    Thirdly, I'm using the UUID for each document, would this be the way to retrieve a specific document.

    Take a look at the read operations done in this doc.

    Sample:

    collection.find(
        new Document("stars", new Document("$gte", 2)
              .append("$lt", 5))
              .append("categories", "Bakery")).forEach(printBlock);