Home  >  Article  >  Web Front-end  >  nodejs addition, deletion and modification

nodejs addition, deletion and modification

王林
王林Original
2023-05-17 11:49:07581browse

Node.js is a JavaScript running environment built on the Chrome V8 engine, which allows JavaScript code to run on the server side. It is very common to perform addition, deletion and modification operations in Node.js. This article will introduce how to use Node.js to perform addition, deletion and modification operations.

1. Add data

To add data in Node.js, you need to use a database module. Commonly used ones include Mongoose, Sequelize, etc. This article uses Mongoose as an example to introduce.

  1. Install Mongoose

Run the following command in the command line to install:

npm install mongoose --save
  1. Connect to the database

First you need to connect to the MongoDB database, the code is as follows:

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

Among them, mongodb://localhost/test means connecting to the local MongoDB database named test.

  1. Create data model

To use Mongoose, you need to define the data model first. You can create a user.js# in the models folder. ## file, the code is as follows:

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  name: String,
  age: Number
});

module.exports = mongoose.model('User', userSchema);

Among them,

userSchema defines the user's name and age attributes.

    Add data
You can add data in the main file through the following code:

const User = require('./models/user');
const user = new User({
  name: 'John',
  age: 25
});

user.save((err) => {
  if (err) {
    console.log(err);
  } else {
    console.log('User created');
  }
});

Among them,

user.save() Save the newly added user data to the MongoDB database.

2. Deleting data

Deleting data in Node.js also requires the use of the database module, taking Mongoose as an example.

    Delete data
You can delete data through the following code in the main file:

const User = require('./models/user');

User.deleteOne({ name: 'John' }, (err) => {
  if (err) {
    console.log(err);
  } else {
    console.log('User deleted');
  }
});

Among them,

User.deleteOne() means to delete the data whose name is John in the user attribute.

3. Modify data

Modifying data in Node.js also requires the use of the database module, which will be introduced using Mongoose as an example.

    Modify data
You can modify the data through the following code in the main file:

const User = require('./models/user');

User.findOneAndUpdate({ name: 'John' }, { age: 26 }, (err, user) => {
  if (err) {
    console.log(err);
  } else {
    console.log('User updated');
  }
});

Among them,

User.findOneAndUpdate() means to find the data whose name is John in the user attribute, and modify the age attribute to 26.

4. Summary

This article introduces how to add, delete and modify data in Node.js. In actual application development, the code implementation may be slightly different depending on specific business requirements and database modules. I hope this article can help beginners get started with Node.js.

The above is the detailed content of nodejs addition, deletion and modification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn