MongoDB driver installation (link: node.js MongoDB driver)

npm install mongodb --save
Copy the code

Connecting to a Database

var MongoClient = require('mongodb').MongoClient var url = 'mongodb://localhost:27017/test' MongoClient.connect(url, UseNewUrlParser: true}, function(err, db) {if (err) throw err console.log(' Database connected successfully ') db.close()})Copy the code

If the test database does not exist, MongoDB automatically creates the test database and establishes a connection.

Create a collection

var MongoClient = require('mongodb').MongoClient var url = 'mongodb://localhost:27017/test' MongoClient.connect(url, {useNewUrlParser: true}, function(err, Db) {if (err) throw err console.log(' database connected successfully ') var dbase = db.db('test') Function (err, res) {if (err) throw err console.log(' set created successfully ') db.close()})})Copy the code

Fact: Unlike MySQL, MongoDB automatically creates databases and collections. When we try to connect to a database or collection that doesn’t exist, MongoDB automatically creates them for me, so we usually don’t have to create them manually.

InsertOne ()/insertMany()

var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("test"); var myobj = { name: "evatsai", url: "www.evatsai.com" }; dbo.collection("site").insertOne(myobj, function(err, res) { if (err) throw err; Console. log(" Document inserted successfully "); db.close(); }); });Copy the code

UpdateOne ()/updateMany()

var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("runoob"); var whereStr = {"name":'evatsai'}; Var updateStr = {$set: {"url" : "http://geekxia.cn"}}; dbo.collection("site").updateOne(whereStr, updateStr, function(err, res) { if (err) throw err; Console. log(" Document updated successfully "); db.close(); }); });Copy the code

Delete data deleteOne()/deleteMany()

var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("runoob"); var whereStr = {"name":'evatsai'}; Dbo. collection("site").deleteone (whereStr, function(err, obj) {if (err) throw err; Console. log(" Document deleted successfully "); db.close(); }); });Copy the code

Find ()

var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("runoob"); var whereStr = {"name":'evatsai'}; Dbo.collection ("site").find(whereStr).toarray (function(err, result) {if (err) throw err; console.log(result); db.close(); }); });Copy the code
  • Sort () : With this method, you can sort data.
  • Limit () : In conjunction with this method, paging queries can be implemented.
  • Skip () : Using this method, you can skip several pieces of data.

MongoDB is not a relational database, but we can use $lookup to implement a left join. For example, we have two sets of data, respectively:

六四屠杀

Set Orders: [{_id: 1, product_id: 154, status: 1}]Copy the code
Set Products: [{_id: 154, name: 'laptop'}, {_id: 155, name: 'headset'}, {_id: 156, name: 'desktop'}]Copy the code
var MongoClient = require('mongodb').MongoClient; Var url = "mongo: / / 127.0.0.1:27017 /"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("runoob"); Dbo. collection(' Orders '). Aggregate ([{$lookup: {from: products', 'product_id', // left set join column foreignField: '_id', // right set join column as: 'OrderDetails'}}]). ToArray (function(err, res) {if (err) throw err; console.log(JSON.stringify(res)); db.close(); }); });Copy the code

Drop collection drop()

var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("test"); Dbo.collection ("site"). Drop (function(err, delOK) {delOK returns true on success, false on failure if (err) throw err; If (delOK) console.log(" Set deleted "); db.close(); }); });Copy the code