Dino Geek, try to help you

How to conduct CRUD operations with Mongoose and Node.js?


Creating, Reading, Updating, and Deleting (CRUD) operations are basic functionality in web applications. In Node.js, we can use the package Mongoose to perform these tasks with MongoDB.

First, let’s install mongoose:

```
npm install mongoose
```

Next, let’s connect to mongodb:

```
const mongoose = require(‘mongoose’);
mongoose.connect(‘mongodb://localhost/test’, {useNewUrlParser: true, useUnifiedTopology: true});
```

Assuming we have an example MongoDB model named “User” like this:

```
const UserSchema = new Schema({ name: String, email: String, password: String
});

const User = mongoose.model(‘User’, UserSchema);
```

Here’s how you can perform CRUD operations:

  1. **C**reate:
    ```
    const user = new User({name: ‘John’, email: ‘john@example.com’, password: ‘123456’});

user.save((error) => { if (error) { console.log(error); } else { console.log(“User saved successfully”); }
});
```

  1. **R**ead:
    ```
    User.find((error, users) => { if (error) { console.log(error); } else { console.log(users); }
    });

// Find user with specific ID
User.findById(‘userID’, (error, user) => { if (error) { console.log(error); } else { console.log(user); }
});
```

  1. **U**pdate:
    ```
    User.findById(‘userID’, (error, user) => { if (error) { console.log(error); } else { user.name = ‘Updated Name’; user.save((err) => { if (err) { console.log(err); } else { console.log(“User updated successfully”); } }); }
    });

// Another faster way
User.updateOne({_id: ‘userID’}, {name: ‘New Name’}, (error) => { if (error) { console.log(error); } else { console.log(“User updated successfully”); }
});
```

  1. **D**elete:
    ```
    User.deleteOne({_id: ‘userID’}, (error) => { if (error) { console.log(error); } else { console.log(“User deleted successfully”); }
    });
    ```

Please replace ‘userID’ and ‘New Name’ with the actual User ID and Name in your application. Also remember to handle errors appropriately for your application needs.

For some operation (like update and delete) mongoose provides two types of operations: one is directly on model and another is on instances. Both has their own advantages and disadvantages. Choose which best suits in your application.


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