0% found this document useful (0 votes)
8 views2 pages

MongoDB and React QnA

The document outlines fundamental operations in MongoDB, including creating a database, a collection, and inserting multiple documents. It explains that a database is created upon data insertion and collections can be created explicitly or automatically. Additionally, it highlights the flexibility of MongoDB in handling different document structures within the same collection.

Uploaded by

Swag Bgm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

MongoDB and React QnA

The document outlines fundamental operations in MongoDB, including creating a database, a collection, and inserting multiple documents. It explains that a database is created upon data insertion and collections can be created explicitly or automatically. Additionally, it highlights the flexibility of MongoDB in handling different document structures within the same collection.

Uploaded by

Swag Bgm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1) MongoDB: Create Database, Collection, and Insert Multiple Documents

To perform fundamental operations in MongoDB, such as creating a database, a collection, and

inserting multiple documents, the MongoDB shell or a MongoDB client (like Compass or a Node.js

driver) can be used. Here's how each operation is handled:

1. **Creating a Database:**

MongoDB automatically creates a database when data is inserted into it. You can switch to the

desired database using:

```js

use myDatabase

```

2. **Creating a Collection:**

Collections are created when you insert the first document into them, but they can also be explicitly

created:

```js

db.createCollection("products")

```

3. **Inserting Multiple Documents:**

You can insert multiple documents into a collection using `insertMany()`:

```js

db.products.insertMany([

{ name: "Laptop", category: "Electronics", price: 1200, stock: 20 },

{ name: "Keyboard", category: "Electronics", price: 100, stock: 50 },

{ name: "Chair", category: "Furniture", price: 150, stock: 15 },

{ name: "Notebook", category: "Stationery", price: 5, stock: 200 }


])

```

Each document is a JSON-like object that MongoDB stores internally in BSON format. The `_id` field

is automatically generated unless specified. MongoDBs flexibility allows different documents within

the same collection to have different fields or structures.

These basic operations form the foundation of MongoDB data handling. Combined with powerful

querying, indexing, and aggregation capabilities, MongoDB offers robust support for modern,

scalable applications.

You might also like