Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
1. Use the MongoDb database in Nodejs
Nodejs official document of mongodb database operations
npm install mongodb --save
/ / or
cnpm install mongodb --save
/ / or
yarn add mongodb
Copy the code
/ / into the mongo
const {MongoClient} = require('mongodb');
// Define the address of the database connection
const url = 'mongodb://localhost:27017';
// const url = 'mongodb://admin:123456@localhost:27017/'; There is a password connection mode Admin indicates the user name, and 123456 indicates the password
// Define the database to operate on
const dbName = 'itying';
MongoClient.connect(url, { useUnifiedTopology: true },(err,client) = >{
if(err){
console.log(err);
return;
}
console.log("Connection successful");
// Get the db object
const db = client.db(dbName);
// Close the database when you are done with it
client.close();
})
Copy the code
2. Nodejs searches for the MongoDb database data
const {MongoClient} = require('mongodb');
const url = 'mongodb://localhost:27017';
// const url = 'mongodb://admin:123456@localhost:27017/'; There is a password connection mode Admin indicates the user name, and 123456 indicates the password
// Define the database to operate on
const dbName = 'itying';
MongoClient.connect(url, { useUnifiedTopology: true },(err,client) = >{
if(err){
console.log(err);
return;
}
console.log("Connection successful");
// Get the db object
const db = client.db(dbName);
// Find data
db.collection('user').find({"age":12}).toArray((err,data) = > {
console.log(data)
// Close the database when you are done with itclient.close(); })})Copy the code
3. Nodejs adds data to MongoDb
const {MongoClient} = require('mongodb');
const url = 'mongodb://localhost:27017';
// const url = 'mongodb://admin:123456@localhost:27017/'; There is a password connection mode Admin indicates the user name, and 123456 indicates the password
// Define the database to operate on
const dbName = 'itying';
MongoClient.connect(url, { useUnifiedTopology: true },(err,client) = >{
if(err){
console.log(err);
return;
}
console.log("Connection successful");
// Get the db object
const db = client.db(dbName);
// Add data
db.collection('user').insertOne({"name":"Tester"."age":17},(err,result) = >{
if(err){
console.log(err);
return;
}
console.log(result);
client.close()
})
})
Copy the code
4. Nodejs Modifies MongoDb data
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
// const url = 'mongodb://admin:123456@localhost:27017/'; There is a password connection mode Admin indicates the user name, and 123456 indicates the password
// Define the database to operate on
const dbName = 'itying';
MongoClient.connect(url, { useUnifiedTopology: true },(err,client) = >{
if(err){
console.log(err);
return;
}
console.log("Connection successful");
// Get the db object
const db = client.db(dbName);
// Modify the data
db.collection('user').updateOne({"name": "xiaoming"}, {$set: {"age": 20}},(err,result) = >{
if(err){
console.log(err);
return;
}
console.log(result);
client.close()
})
})
Copy the code
5. Nodejs Deletes MongoDb data
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
// const url = 'mongodb://admin:123456@localhost:27017/'; There is a password connection mode Admin indicates the user name, and 123456 indicates the password
// Define the database to operate on
const dbName = 'itying';
MongoClient.connect(url, { useUnifiedTopology: true },(err,client) = >{
if(err){
console.log(err);
return;
}
console.log("Connection successful");
// Get the db object
const db = client.db(dbName);
// Delete a piece of data
db.collection('user').deleteOne({"name": "xiaoming"},(err,result) = >{
if(err){
console.log(err);
return;
}
console.log(result);
client.close()
})
// Delete multiple pieces of data
db.collection('user').deleteMany({"name": "Designer"},(err,result) = >{
if(err){
console.log(err);
return;
}
console.log(result);
client.close()
})
})
Copy the code