Skip to content

JulioFerrero/mongoDBLinux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongoDB guide for GNU+Linux with free Atlas

The creators of MongoDB grant a free server to be able to use their technology, in our case we are going to take advantage of it to learn and do tests.

First we must register on their website: First image

We give our project a name and specify the language that we are going to use (in our case Java): Second Image

We choose the free plan: Third image

We select the server provider and the location: Fourth image

We just have to wait for it to finish starting and we can use our new server: Fifth image

That's all friends! we add our beautiful IP in the white list and we can access it through Java or Mongo Shell: Sixth image

If you want to learn how to connect through java, take a look at the code in this repository but here is some sample code. But be careful, to start using mongo in Java we must install its JAR, in my case through POM.xml

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>4.2.0</version>
</dependency>

Okay! First we have to add the ConnectionString of our server:

ConnectionString connString = new ConnectionString(
                    "mongodb+srv://USERINMYSERVER:PASSWORD@URLOFMYSERVER/?retryWrites=true&w=majority"
            );

In my case it would be like this:

ConnectionString connString = new ConnectionString(
                "mongodb+srv://dbUser:jak1k0@cluster0.cjxmj.mongodb.net/?retryWrites=true&w=majority"
        );

We add the options with MongoClientSettings:

MongoClientSettings settings = MongoClientSettings.builder()
                .applyConnectionString(connString)
                .retryWrites(true)
                .build();

And we apply them:

MongoClient mongoClient = MongoClients.create(settings);

And finally we specify the database and the collection:

MongoDatabase database = mongoClient.getDatabase("nueva");
MongoCollection<Document> collection = database.getCollection("profes");

Y con esto y un bizcocho (as we say in Catalonia) we would already have it, we can use collection.deleteOne to delete or collection.insertOne to add a new document. If you want to learn more, you have some code examples from this repository or in the MongoDB documentation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages