Dino Geek, try to help you

How to insert data into MongoDB?


Before you can insert data into MongoDB, you need to create both a MongoDB database and collection.

1. Create a MongoDB database: The `use DATABASE_NAME` command is written to create a database in MongoDB. If the database does not exist, MongoDB will create it.

```
use myDb
```
1. Create a MongoDB Collection: The `db.createCollection(name, options)` is used to create collection.

```
db.createCollection(‘myCollection’)
```

1. Insert Data into MongoDB

After creating both a MongoDB database and collection, you can insert data into MongoDB.

- Insert One Document: To insert a single document into a collection, you can use the `insertOne()` method.

```
db.myCollection.insertOne({name: ‘John’, age: 30, profession: ‘Developer’})
```

- Insert Multiple Documents: To insert multiple documents into a collection you can use the `insertMany()` method.

```
db.myCollection.insertMany([ {name: ‘John’, age: 30, profession: ‘Developer’}, {name: ‘Jane’, age: 28, profession: ‘Designer’}, {name: ‘Doe’, age: 26, profession: ‘Tester’}
])
```
These commands will insert one or many documents to the `myCollection` collection. If the collection doesn’t exist in the database, then MongoDB will create this collection and then insert a document into it.

In MongoDB, you don’t need to create a collection. MongoDB creates collections automatically, when you insert some documents.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use