Join Regular Classroom : Visit ClassroomTech

MongoDB – codewindow.in

Related Topics

React JS

Introduction to React.js
React JS Page 1
React JS Page 2
React JS Page 3

Components in React.js
React JS Page 4
React JS Page 5

Virtual DOM in React.js
React JS Page 6
React JS Page 7

State and Props in React.js
React JS Page 8
React JS Page 9

React Router
React JS Page 10
React JS Page 11

React Hooks
React JS Page 12
React JS Page 13

Redux in React.js
React JS Page 14
React JS Page 15

Context API in React.js
React JS Page 16
React JS Page 17

React with Webpack and Babel
React JS Page 18
React JS Page 19

Testing in React.js
React JS Page 20
React JS Page 21

Deployment and Optimization in React.js
React JS Page 22
React JS Page 23

Emerging Trends and Best Practices in React.js
React JS Page 24
React JS Page 25

Node JS

Introduction
Node.js Page 1
Node.js Page 2

Node.js Architecture and Event-Driven Programming
Node.js Page 3
Node.js Page 4

Modules and Packages in Node.js
Node.js Page 5
Node.js Page 6

File System and Buffers in Node.js
Node.js Page 7
Node.js Page 8

HTTP and Networking in Node.js
Node.js Page 9
Node.js Page 10

Express.js and Web Applications
Node.js Page 11
Node.js Page 12

Databases and ORMs in Node.js
Node.js Page 13
Node.js Page 14

RESTful APIs in Node.js
Node.js Page 15
Node.js Page 16

Testing and Debugging in Node.js
Node.js Page 17

Deployment and Scalability in Node.js
Node.js Page 18
Node.js Page 19

Emerging Trends and Best Practices in Node.js
Node.js Page 20
Node.js Page 21

Performance Optimization in Node.js
Node.js Page 22
Node.js Page 23

MongoDB

use mydatabase

3. If the specified database already exists, it will be selected. Otherwise, MongoDB will create a new database when you start adding data to it.

4. You can now start adding data to your new database or create collections within it.

db.collectionName.insertOne({ key: "value" })

Replace collectionName with the name you want to give to your collection and { key: "value" } with the data you want to insert.

5. MongoDB creates the new database when you first store data into it. Until then, the database will not be listed in the output of the show dbs command. To see the list of databases, you can use the following command:

show dbs

The new database will appear in the list once it contains data.

  • Remember, MongoDB is a schema-less database, which means that you don’t need to define the structure of the database or its collections explicitly before using them. You can start inserting data right away, and MongoDB will create collections and the database on the fly as necessary.

use mydatabase

If the specified database already exists, it will be selected. Otherwise, MongoDB will create a new database when you start adding data to it.

3. You can now start adding data to your new database or create collections within it. For instance, let’s create a collection named “mycollection” and insert a document into it:

db.mycollection.insertOne({ name: "John", age: 30 })

This command will create the “mycollection” collection within the “mydatabase” database and insert a document with the fields “name” and “age” into it.

4. To verify that the database has been created and switch to it, you can use the show dbs command. It will display a list of all available databases:

show dbs

The output will include the “mydatabase” in the list, and you can switch to it again using the use command:

use mydatabase

Now you are working within the “mydatabase” database and can perform further operations on its collections.

Remember, until you store data in the database, it may not appear in the show dbs output. MongoDB creates the new database when you start adding data to it.

db.createCollection(collectionName, options)

Here’s a breakdown of the parameters:

  • collectionName: The name of the collection you want to create. It should be a string.

  • options (optional): An object that specifies additional options for the collection. This parameter is optional, and you can omit it if you don’t need to specify any options.

Let’s see an example of creating a new collection called “mycollection” with some options:

db.createCollection("mycollection", { capped: true, size: 8192, max: 100 })

In this example, we’re creating a collection named “mycollection” with the following options:

  • capped: true – This option creates a capped collection, which is a fixed-size collection that maintains the insertion order of documents. When the collection reaches its maximum size (size option), older documents are overwritten with new ones.

  • size: 8192 – This option specifies the maximum size, in bytes, for the capped collection. In this case, it’s set to 8192 bytes (8KB).

  • max: 100 – This option sets the maximum number of documents the capped collection can hold. Once this limit is reached, older documents are overwritten with new ones.

You can customize the options based on your requirements or create a collection without any options by omitting the options parameter.

Note that when you insert documents into a collection, MongoDB will create the collection implicitly if it doesn’t exist already. The db.createCollection() method provides explicit control over the collection’s options and allows you to create it before inserting any documents if needed.

use mydatabase

If the database exists, the shell prompt will change to mydatabase, indicating its existence. If it doesn’t exist, MongoDB will create it once you store data in it.

2. List All Available Databases:

    • In the MongoDB shell, use the show dbs command to list all available databases in the MongoDB instance.

    • This command displays a list of databases along with their sizes.

show dbs

The output will include all the existing databases, along with their sizes in bytes. However, it’s important to note that databases without any data (no documents inserted) may not appear in the show dbs output.

Remember that in MongoDB, databases are created implicitly when data is stored in them or explicitly when you switch to a non-existing database and perform data operations. The 'show dbs' command provides an overview of all databases that have data stored in them.

      

Popular Category

Topics for You

React JS

Introduction to React.js
React JS Page 1
React JS Page 2
React JS Page 3

Components in React.js
React JS Page 4
React JS Page 5

Virtual DOM in React.js
React JS Page 6
React JS Page 7

State and Props in React.js
React JS Page 8
React JS Page 9

React Router
React JS Page 10
React JS Page 11

React Hooks
React JS Page 12
React JS Page 13

Redux in React.js
React JS Page 14
React JS Page 15

Context API in React.js
React JS Page 16
React JS Page 17

React with Webpack and Babel
React JS Page 18
React JS Page 19

Testing in React.js
React JS Page 20
React JS Page 21

Deployment and Optimization in React.js
React JS Page 22
React JS Page 23

Emerging Trends and Best Practices in React.js
React JS Page 24
React JS Page 25

Node JS

Introduction
Node.js Page 1
Node.js Page 2

Node.js Architecture and Event-Driven Programming
Node.js Page 3
Node.js Page 4

Modules and Packages in Node.js
Node.js Page 5
Node.js Page 6

File System and Buffers in Node.js
Node.js Page 7
Node.js Page 8

HTTP and Networking in Node.js
Node.js Page 9
Node.js Page 10

Express.js and Web Applications
Node.js Page 11
Node.js Page 12

Databases and ORMs in Node.js
Node.js Page 13
Node.js Page 14

RESTful APIs in Node.js
Node.js Page 15
Node.js Page 16

Testing and Debugging in Node.js
Node.js Page 17

Deployment and Scalability in Node.js
Node.js Page 18
Node.js Page 19

Emerging Trends and Best Practices in Node.js
Node.js Page 20
Node.js Page 21

Performance Optimization in Node.js
Node.js Page 22
Node.js Page 23

Go through our study material. Your Job is awaiting.

Recent Posts
Categories