mongodb is a type of document databases, which store and query data basically json format, mongodb and nodejs like eggs and tofu natural fit.
Before using nodejs operating mongodb need to understand the hierarchy of mongodb:
- system (mongodb will install some database)
- admin
- userdefind (user-defined database)
- database (database name)
- Collections (set of tables)
- collection (the collection)
- functions
- users
- Collections (set of tables)
- database (database name)
mongodb for nodejs provided official drivers mongdb npm tool can be used directly loaded. Since the model nodejs characteristics, so using mongodb all related operations or by way of a callback promise manner.
Built environment
- Installation nodejs
- nPM heat
- package.json add dependencies
Get all of the database
api:admin.listDatabases
//回调版
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
client.db("").admin().listDatabases(function (err, resp) {
resp.databases.forEach(function (value, index, arr) {
console.log(value.name);
})
})
});
//promise版
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
client.db("").admin().listDatabases().then(function (data) {
data.databases.forEach(function (value, index, arr) {
console.log(value.name);
})
}, function (err) {
console.log(err.message);
})
});
Create a database
mongodb when the database does not exist, it will automatically create the database, therefore, you can not deliberately created to operate the database
Create Collection
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
client.db("example").createCollection("table1").then(function (result) {
}, function (err) {
console.log(err.message);
})
});
Get all collections
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
client.db("example").listCollections().toArray().then(function (tables) {
tables.forEach(function (value, index, ts) {
console.log(value.name);
})
}, function (err) {
console.log(err.message);
})
});
Insert data collection
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
client.db("example").collection("table1").insert({ name: "张三丰" }).then(function (result) {
}, function (err) {
console.log(err.message);
})
});
Gets a collection of all the data
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
client.db("example").collection("table1").find({}).toArray().then(function (result) {
result.forEach(function (value, index, arr) {
console.log(value.name);
});
}, function (err) {
console.log(err.message);
})
});
change the data
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
client.db("example").collection("table1").update({ name: "李斯" }, { age: 100,name: "李斯" }).then(function (result) {
console.log(result.result);
}, function (err) {
console.log(err.message);
})
});
Query by the conditions
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
client.db("example").collection("table1").find({ age: 100 }).toArray().then(function (result) {
result.forEach(function (value, index, arr) {
console.log(value);
})
}, function (err) {
console.log(err.message);
})
});
delete data
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
client.db("example").collection("table1").deleteMany({ age: 100 }).then(function (result) {
console.log(result.result);
}, function (err) {
console.log(err.message);
})
});
A simple mongodb data replication project https://github.com/golangaccount/mongodb_simple_clone