Mongodb java tutorial LEARNOVITA

MongoDB and Node.js Tutorial | Create Database

Last updated on 11th Aug 2022, Blog, Tutorials

About author

Jesica Parveen (Full Stack Developer )

Jesica Parveen has 8+ years of full-stack developer expertise in HTML, CSS, JavaScript, jQuery, CSS, SEO, facing UI, and back. He has skills in Servlet and JSP development, CSS, JavaScript, HTML, AJAX, jQuery, EXTJS, OSGi/FELIX, CMS development experience, Java Content Repository (JCR), and Eclipse IDE.

(5.0) | 18739 Ratings 2164

MongoDB :

MongoDB offers basic and advanced concepts of SQL.MongoDB is designed for beginners and professionals.MongoDB is a Nosql database.It’s a C++ document-oriented open-source database.Insert, update, delete, query, sort, limit, create collection, drop collection, etc.

What is MongoDB:

MongoDB is associate degree ASCII text file document info that gives high performance, high handiness, and automatic scaling.MongoDB is a document-oriented database.It’s a 10gen open source product.It is available under General Public license for free, and it is available under Commercial license from the manufacturer.The manufacturing company 10gen has explained MongoDB as:”MongoDB could be a scalable , open supply, high performance, document-oriented info.” – 10genMongoDB was designed to figure with goods servers.MongoDB uses commodity servers.It is used by companies of all sizes, across all the industries.

History of MongoDB:

  • The initial development of MongoDB began in 2007 once the corporate was building a platform as a service almost like window azure.
  • MongoDB was developed by a NewYork based company named 10gen which is now known as MongoDB Inc.
  • Initially a PAAS (Platform as a Service).
  • Later in 2009, it was introduced in the market as an open source database server that was managed and supported by MongoDB Inc.
  • The first prepared production of MongoDB has been thought-about from version one.4 that was free in March 2010.

All modern applications need big data, quick features development, flexible deployment, and the older database systems are not competent enough, so MongoDB was needed.

The main purpose of building MongoDB is:

  • Scalability
  • Performance
  • High Availability
  • Scaling from single server deployments to Large, complicated multi-site architectures.
  • Key points of MongoDB
  • Develop Faster
  • Deploy Easier
  • Scale Bigger

MongoDB Java:

  • MongoDB Java Driver Download
  • Creating MongoDB Java Connection
  • Connection to MongoDB Database
  • MongoDB and Collections

Features of MongoDB:

These are important features of MongoDB:

    1. 1. Support ad hoc queries: In MongoDB, can search by field, range query and it also encourages regular expression searches.
    2. 2. Indexing: All of a document’s fields may be indexed using indexing.
    3. 3. Replication: MongoDB supports the Master Slave replication.One can read and write to a master, but only read and back up data can be done on a slave (not for writes)
    4. 4. Duplication of data: MongoDB can run over multiple servers. Data duplication ensures continued service and data integrity in the event of hardware failure.
    5. 5. Load balancing: It has an automatic load balancing configuration because of information placed in shards.
    6. 6. Encourage map reduce and aggregation tools.
    7. 7. Uses JavaScript instead of the Procedures.
    8. 8. It is a schema-less database written in the C++.
    9. 9. Provides more performance.
    10. 10. Save files of any size simply without complicating the stack.
    11. 11. simple to administer in the case of failures.
    12. 12. It also supports: JSON data model with the dynamic schemasAuto-sharding for the horizontal scalabilityBuilding replication for more availability How many companies are using MongoDB to create new types of applications, high performance and availability.
What is MongoDB

Components of MongoDB:

The important components of MongoDB are:

id: This field represents an individual field in MongoDB. This field is created by default.

Collection: It is a set of MongoDB documents. It exists with one database.

Database: This is the container for collections. Multiple databases can be saved in a mongoDB server.

Document: A record in mongoDB is called a document. It consists of names and values.

Field: It is a name-value pair in the document.

Terminologies:

Understanding concepts in MongoDB becomes simpler if you can compare them to relational database structures.The analogies between Mongo and the traditional MySQL system:

  • Table in MySQL becomes the Collection in Mongo
  • Row becomes the Document
  • Column becomes the Field
  • Joins are explained as linking and embedded documents
  • This is an easy way to look at the MongoDB core concepts of course, but nevertheless useful.

Connecting to a Database:

It is interesting , we don’t need to create one. When Mongo sees that database not exist, it will create it for us:

  • DB database = mongoClient.getDB(“myMongoDb”);
  • Sometimes, by default, MongoDB runs in authenticated mode.
  • In that case, we need to authenticate while connecting to the database.
  • can do it as presented below:
  • MongoClient mongoClient = new MongoClient();
  • DB database = mongoClient.getDB(“myMongoDb”);
  • boolean auth = database.authenticate(“username”, “pwd”.toCharArray());

Establishing connections to database:

For making the connection, to mention the database name. MongoDB creates a database by default if no name is mentioned:

    1. 1. Firstly, import the required libraries for establishing a connection.
    2. 2. Here, “MongoClient” is used to make the client for the database.
    3. 3. “MongoCredential” is used for making the credentials.
    4. 4. And finally, to access the database “MongoDatabase” is used.
    5. 5.Username will be: “GFGUser” and the database name will be “mongoDb“.
    6. 6. The function “.toCharArray()” is used to convert the password into a character array.
    7. 7.The function “.getDatabase()” is used for getting the database.

MongoDB Data Types:

Following is a usable data type in MongoDB:

    Data TypesDescription
    String String is the generally used datatype. It is used to save data. A string must be UTF 8 valid in the mongodb.
    Integer Integer is used to save the numeric value. It can be 32 bit or 64 bit depending on the server.
    Boolean This datatype is used to save boolean values. It just displays YES/NO values.
    Double Double datatype saves floating point values.
    Min/Max Keys This data type compared a value against the lowest and highest bson elements.
    Arrays This datatype is used to save a list or multiple values into a single key.
    Object Object datatype is used for the embedded documents.
    Null It is used to save null values.
    Symbol It is commonly used for languages that use a specific type.
    Date This data type saves the current date or time in unix time format. It makes it possible to specify its own date time by creating an object of date and passing the value of date, month, year into it.

Syntax:

1. db.aggregate( [ <"pipeline"> ], { <"options"> } ):

The pipeline parameter does not need any underlying collection and always starts with a compatible stage, such as $currentOp or $listLocalSessions. It is an array of levels that will be executed.

#3. db.cloneDatabase(“hostname”):

The clonedatabase method copies the particular database to the current database and assumes that the database at the remote location has the same name as the current database.The hostname parameter contains the hostname of the database that you want to copy.

MongoDB

#4. db.commandHelp(command):

We have the help option for the particular database command using the commandHelp method.The command parameter contains the name of the database command.

#5. db.createCollection(name, options):

A new gathering or view will be created using this method.The createCollection method is used primarily for making new collections that use specific options when the collection is first referenced in a command.

#6. db.createView():

When applying the particular aggregation pipeline to the collection, the createView method creates a new view for the collectionThe method can be computed during the read operations and acts as the read-only operations.The views can be created in the same database of the source gathering to execute read operations as a part of the underlying aggregation pipeline.

Syntax:

1.db.createView(<"view'>, <'source'">, <"pipeline">, <"options">)

#7. db.dropDatabase(<'writeConcern">)

The drop method removed the specified database and the associated data files.

For example –

Use <"database"> operation to switch the current database to the temporary database.Use the db.dropDatabase() method to drop the temporary database.

    1. 1. use temp
    2. 2. db.dropDatabase()

#8. db.getLogComponents()

  • The getLog method returns a current stilted setting.
  • The method finds the amount of Log Messages produced by MongoDB.
  • For every log message component.

Are you looking training with Right Jobs?

Contact Us

Popular Courses