tutorial
This is the first chapter of the MongoDB Tutorial for Node.js Developers, which relies heavily on the Authoritative MongoDB Guide (2nd Edition) by Kirstina Chodorow (O’Reilly, 2013). The version of MongoDB used in the authoritative guide is 2.4.x. While actually writing the user code, some of the syntax is no longer available, and the environment used for this tutorial is the latest version of MongoDB 5.0.x. Besides, I am a Node.js developer myself, and all the in-app codes in the tutorial are written by Node.js + Mongoose.
Install the mongo
Install MongoDB in Windows, Linux, MacOS and other environments. You can download and install MongoDB on the MongoDB official website – MongoDB Community Download. In this article I prefer readers to try to install MongoDB using Docker for two reasons:
1. Simple: With a single command, you can run a MongoDB database container through mirroring
2. Easy debugging: it may be necessary to add, delete, change and check the same data in the tutorial. Docker can create multiple non-interfering containers to facilitate debugging and comparison.
For details, see “Installing MongoDB with Docker” at the end of this article.
Data source (hot spot of STATION B)
The database data in this tutorial comes from the backend interface data of the “General Hot” at Site B. The author provides 25300 pieces of document data for the convenience of adding, deleting, modifying and checking data in the tutorial. The following takes a data from the database:
{
"_id" : ObjectId("6123b52a64ceb40e2917c933"),
"videos" : 1."tid" : 17."desc" : ""."duration" : 1537."aid" : 675061011."tname" : "Single player game"."copyright" : 1."pic" : "http://i1.hdslb.com/bfs/archive/7b9bff6a7e31bc55fc6a80341a23c43225b5f24e.jpg"."title" : "Three Kingdoms and Four Continents cannot be saved."."pubdate" : ISODate("The 1970-01-19 T20: even 902 z"),
"ctime" : ISODate("The 1970-01-19 T20: even 902 z"),
"state" : 0."owner" : {
"mid" : 4564056."name" : "Ryuzaki Lollipop"."face" : "http://i0.hdslb.com/bfs/face/74b6d0663e92c0595c40033a1f3495aff7d27f4b.jpg"
},
"stat" : {
"aid" : 675061011."view" : 1255439."danmaku" : 8719."reply" : 6384."favorite" : 30489."coin" : 81238."share" : 12405."now_rank" : 0."his_rank" : 46."like" : 99243."dislike" : 0
},
"dynamic" : "I originally wrote 8000 words of copy, but as a result, I was tired out and fell ill, so I deleted more than 1000 words. During the process, my computer crashed once, and I lost the copy and other saved files that I had written for one and a half hours. It can be said that this is the most courageous video ever.."cid" : 395060770."dimension" : {
"width" : 1920."height" : 1080."rotate" : 0
},
"short_link" : "https://b23.tv/BV18U4y1j765"."short_link_v2" : "https://b23.tv/BV18U4y1j765"."first_frame" : "http://i0.hdslb.com/bfs/storyff/n210823a22pk2invww0sv02xkt574ew8_firsti.jpg"."bvid" : "BV18U4y1j765"."season_type" : 0."rcmd_reason" : {
"content" : "Million Play"."corner_mark" : 0
},
"update_time" : ISODate("The 2021-08-23 T14:3. 392 z"),
"__v" : 0
}
Copy the code
You can download the data file bilihot.json provided by me through Baidu web disk and import it into MongoDB.
Baidu web disk download address:
- Link: pan.baidu.com/s/193KNiPwi…
- Extraction code: 5G9K
Use mongoimport to import bilihot.json:
$ mongoimport -d bilibili_hot -c hotspots --file /bilihot.json --type json
Copy the code
Parameter Description:
-d
: Database name-c
: Collection name--file
: Indicates the path of the file to be imported--type
: Indicates the file type to be imported. The default value is JSON
A cloud server is recommended
For most front-end developers, it is recommended not only to run the code locally, but also to buy a cloud server, install MongoDB and run it using a remote command line connection. This is conducive to learning Linux related commands and adapting to the back-end operating environment.
Ali Cloud – cloud server latest offers address
Mongo basis
The database
In MongoDB, multiple documents form collections, and collections can form databases. A MongoDB instance can host multiple databases, each with zero or more collections. Each database has its own permissions, and even on disk, different databases are placed in different files.
In MongoDB, we can use db_name to switch the database. For example, if our database name is bilibili_hot in this tutorial, you can use the following command to access the database.
> use bilibili_hot
Copy the code
The document
Document is a core concept in MongoDB. If you are familiar with relational databases such as MySQL, you can compare a document to a line. A document is represented as an object in a JavaScript document. For example, a hot data from site B just stored in the database is a document:
{
"title" : "Three Kingdoms and Four Continents cannot be saved."
"pubdate" : ISODate("The 1970-01-19 T20: even 902 z"),
"ctime" : ISODate("The 1970-01-19 T20: even 902 z"),
"duration" : 1537. }Copy the code
MongoDB documents are type – and case-sensitive, and cannot have duplicate keys. This is basically the same concept as objects in JavaScrit, which is easy for Node.js developers to understand. For example, the following two documents are different:
{
"title" : "Three Kingdoms and Four Continents cannot be saved."
}
{
"Title" : "Three Kingdoms and Four Continents cannot be saved."
}
Copy the code
The following two documents are also different:
{
"duration" : 1537
}
{
"duration" : "1537"
}
Copy the code
A collection of
A collection is a group of documents. One or more collections make up a MongooDB database. We can compare a collection to a table in a relational database.
The English word for collection is collection, which is often used in MongoDB operations:
- For example, to display all collections:
> show collections
Copy the code
- Create a collection
statistics
> db.createCollection('statistics')
Copy the code
- The query
hotspots
All document data in the collection:
> db.getCollection('hotspots').find({})
Copy the code
The collection in MongoDB is in dynamic mode, which means that the structure and type of documents in the collection can be various. The following two documents can be placed in the same collection, one file stores the name of the video, and the other file stores the number of times the video is played.
{ "title": "Three Kingdoms and Four Continents cannot be saved." },
{ "view": 1280 }
Copy the code
In the two documents above, not only do the value types differ, but also the keys. As an experienced programmer, you would never organize documents in collections like this. In view of the above situation, we suggest using different sets to store the basic information and statistics of videos (number of plays, likes, coins, etc.).
mongod
Start MongoDB Run the mongod command to start the MongoDB server. Mongod can use many configurable items when starting the MongoDB server.
$ mongod
Copy the code
--dbpath
: Specifies a directory as the data directory. The default directory is/data/db
--port
: Specifies the port number that the server listens to, which mongod uses by default27017
port--fork
: callfork
Create a child process to run MongoDB in the background. To enable the--fork
Must be enabled at the same time--logpath
--logpath
: Specifies a file to output information, rather than on the command line.--config
: Loads additional configuration files. Options not specified on the command line will use secondary parameters from the configuration file.
Stop MongoDB The command to shut down a running server is an administrator command. First we need to switch to the admin database in the Mongo Shell.
> use admin
Copy the code
To use the db. ShutdownServer ()
> db.shutdownServer()
Copy the code
mongo
By executing Mongo on the command line, MonGOSH (also known as MongoDB Shell) can be started. It is the most important tool for us to learn MongoDB, and Shell is used to complete most MongoDB operations. Before running Mongo, please run mongod to run MongoDB server, then shell will automatically connect to MongoDB server.
$ mongo
MongoDB server version: 5.0.2
---
>
Copy the code
Shell is an independent MongoDB client. When started, shell will connect to test database of MongoDB server. We can use db command to check which database is currently used:
- Viewing the Current Database
db
> db
test
Copy the code
- View all current databases
show dbs
> show dbs
admin 0.000GB
bilibili_hot 0.015GB
config 0.000GB
local 0.000GB
Copy the code
- Use a database
Use Database name
, such as usingbilibili_hot
The database
> use bilibili_hot
switched to db bilibili_hot
Copy the code
CRUD
Create a document
The insert() method is used to insert documents into the collection, and we insert a piece of data under the Mems collection
> db.hotspots.insert({
videos: 1,
tid: 17,
desc: "",
duration: 1537,
aid: 675061011,
tname: "Single player game",
copyright: 1,
pic: "http://i1.hdslb.com/bfs/archive/7b9bff6a7e31bc55fc6a80341a23c43225b5f24e.jpg",
title: "Three Kingdoms and Four Continents cannot be Saved" (New),
pubdate: new Date("The 1970-01-19 T20: even 902 z"),
ctime: new Date("The 1970-01-19 T20: even 902 z"),
state: 0,
owner: {
mid: 4564056,
name: "Ryuzaki Lollipop",
face: "http://i0.hdslb.com/bfs/face/74b6d0663e92c0595c40033a1f3495aff7d27f4b.jpg",
},
stat: {
aid: 675061011,
view: 1255439,
danmaku: 8719,
reply: 6384,
favorite: 30489,
coin: 81238,
share: 12405,
now_rank: 0,
his_rank: 46,
like: 99243,
dislike: 0,
},
dynamic:
"I originally wrote 8000 words of copy, but as a result, I was tired out and fell ill, so I deleted more than 1000 words. During the process, my computer crashed once, and I lost the copy and other saved files that I had written for one and a half hours. It can be said that this is the most courageous video ever.,
cid: 395060770,
dimension: {
width: 1920,
height: 1080,
rotate: 0,
},
short_link: "https://b23.tv/BV18U4y1j765",
short_link_v2: "https://b23.tv/BV18U4y1j765",
first_frame:
"http://i0.hdslb.com/bfs/storyff/n210823a22pk2invww0sv02xkt574ew8_firsti.jpg",
bvid: "BV18U4y1j765",
season_type: 0,
rcmd_reason: {
content: "Million Play",
corner_mark: 0,
},
update_time: new Date("The 2021-08-23 T14:3. 392 z")})Copy the code
Query the document
The find() method is used to query documents in a collection, or findOne() if you want to query a document. Let’s find the new document:
> db.hotspots.findOne({ title: 'Three Kingdoms and Four Continents cannot be Saved' (New) })
Copy the code
Modify the document
The update() method is used to modify the document. Let’s change the title field of the document we just added:
> db.hotspots.update(
{ title: "Three Kingdoms and Four Continents cannot be Saved" (New) },
{ $set: { title: "Three Kingdoms and Four Continents cannot be Saved" (Latest addition)}})Copy the code
Delete the document
The remove() method is used to permanently remove the document from the collection. Let’s remove the document we just changed:
> db.hotspots.remove({ title: 'Three Kingdoms and Four Continents cannot be Saved' (Latest addition) })
Copy the code
Mongoose basis
Mongoose is an object model tool that encapsulates MongoDB database operations in the Node environment. Convert the data in the database into JavaScript objects that we can use in our applications.
Install the Mongoose
npm install mongoose --save
Copy the code
Connection
You can connect to MongoDB using the mongoose.connect() method:
const mongoose = require("mongoose");
mongoose
.connect("Mongo: / / 127.0.0.1:27019 / bilibili_hot", { useNewUrlParser: true })
.then(() = > console.log("MongoDB Connected"))
.catch((err) = > console.log(err));
Copy the code
The first parameter of this method is the MongoDB connection address, and the second parameter is an options object, which will be passed by Mongoose to the MongoDB driver. You can find the completed options configuration items in the MongoDB Node.js driver documentation. Here are some important configuration items:
user/pass
: User name and password for authentication. Mongoose unique, equivalent to MongoDB drivenauth.user
andauth.password
Options.useFindAndModify
Default for:true
, is set tofalse
Will makefindOneAndUpdate()
andfindOneAndRemove()
usefindOneAndUpdate()
Rather thanfindAndModify()
.poolSize
: The maximum socket word count that the MongoDB driver will keep open for this connection. By default,poolSize
Is 5.useNewUrlParser
: The underlying MongoDB driver has deprecated its current connection string parser. Because it was a major change, they added oneuseNewUrlParser
Flag that allows users to fall back to the old parser if errors are found in the new parser. You should set,useNewUrlParser: true
.
Schema
Schema is an important concept in Mongoose. Schema defines the structure of documents in a collection. Combined with the data of the database mentioned above, the Schema we defined is as follows:
// HotSpot.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const hotSpotSchema = new Schema({
aid: {
type: Number,
required: true,
},
videos: {
type: Number,
default: 1,
},
tid: {
type: Number,
default: 0,
},
tname: {
type: String,
require: true,
},
copyright: {
type: Number,
dafault: 1,
},
pic: {
type: String,
},
title: {
type: String,
require: true,
},
pubdate: {
type: Date,
},
ctime: {
type: Date,
require: true,
},
desc: {
type: String,
default: "",
},
state: {
type: Number,
},
duration: {
type: Number,
default: 0,
},
owner: {
mid: {
type: Number,
},
name: {
type: String,
},
face: {
type: String,
},
},
stat: {
aid: {
type: Number,
},
view: {
type: Number,
},
danmaku: {
type: Number,
},
reply: {
type: Number,
},
favorite: {
type: Number,
},
coin: {
type: Number,
},
share: {
type: Number,
},
now_rank: {
type: Number,
},
his_rank: {
type: Number,
},
like: {
type: Number,
},
dislike: {
type: Number,
},
},
dynamic: {
type: String,
},
cid: {
type: Number,
},
dimension: {
width: {
type: Number,
},
height: {
type: Number,
},
rotate: {
type: Number,
},
},
short_link: {
type: String,
},
short_link_v2: {
type: String,
},
first_frame: {
type: String,
},
bvid: {
type: String,
},
season_type: {
type: Number,
},
rcmd_reason: {
content: {
type: String,
},
corner_mark: {
type: Number,
},
},
update_time: {
type: Date,
},
});
Copy the code
Model
Model is a constructor compiled from the Schema, which is why the capital letter is used here. Model is responsible for creating and reading documents from MongoDB.
When you call Mongoose.model (), Mongoose will compile a model for you.
const HotSpot = mongoose.model("hotSpot", hotSpotSchema);
Copy the code
- Parameter 1: is the singular name of a collection, that is, when you do not have any collections in your database, when you connect to MongoDB using Mongoose and are ready to insert data, Mongoose will automatically create a name called
hotspots
The collection. - Parameter 2: call
new Schema
Created by theSchema
CRUD
Once the Model is created, you can use the methods corresponding to the Model to manipulate the documents in the collection. Here we briefly introduce the CRUD method in Mongoose.
Create a document
You can create a document with model.create () :
const newVideo = {
videos: 1.tid: 17.desc: "".duration: 1537.aid: 675061011.tname: "Single player game".copyright: 1.pic: "http://i1.hdslb.com/bfs/archive/7b9bff6a7e31bc55fc6a80341a23c43225b5f24e.jpg".title: "Three Kingdoms and Four Continents cannot be Saved" (New).pubdate: new Date("The 1970-01-19 T20: even 902 z"),
ctime: new Date("The 1970-01-19 T20: even 902 z"),
state: 0.owner: {
mid: 4564056.name: "Ryuzaki Lollipop".face: "http://i0.hdslb.com/bfs/face/74b6d0663e92c0595c40033a1f3495aff7d27f4b.jpg",},stat: {
aid: 675061011.view: 1255439.danmaku: 8719.reply: 6384.favorite: 30489.coin: 81238.share: 12405.now_rank: 0.his_rank: 46.like: 99243.dislike: 0,},dynamic:
"I originally wrote 8000 words of copy, but as a result, I was tired out and fell ill, so I deleted more than 1000 words. During the process, my computer crashed once, and I lost the copy and other saved files that I had written for one and a half hours. It can be said that this is the most courageous video ever..cid: 395060770.dimension: {
width: 1920.height: 1080.rotate: 0,},short_link: "https://b23.tv/BV18U4y1j765".short_link_v2: "https://b23.tv/BV18U4y1j765".first_frame:
"http://i0.hdslb.com/bfs/storyff/n210823a22pk2invww0sv02xkt574ew8_firsti.jpg".bvid: "BV18U4y1j765".season_type: 0.rcmd_reason: {
content: "Million Play".corner_mark: 0,},update_time: new Date("The 2021-08-23 T14:3. 392 z")}; HotSpot.create(newVideo,function (err, video) {
if (err) {
console.log(err);
return;
}
console.log(video)
});
Copy the code
Query the document
Model.find(); model.find ();
HotSpot.find({ title: 'Three Kingdoms and Four Continents cannot be Saved' (New)}, function(err, videos) {
if (err) {
console.log(err)
return
}
console.log(videos)
})
Copy the code
Modify the document
We can use model.updateOne () to update documents in the database
HotSpot.updateOne(
{ title: "Three Kingdoms and Four Continents cannot be Saved" (New) },
{ $set: { title: "Three Kingdoms and Four Continents cannot be Saved" (Latest addition)}},function (err, raw) {
if (err) {
console.log(err);
return;
}
console.log(raw); });Copy the code
Delete the document
Let’s use model.deleteOne () to delete the changed document and restore our database to its original state:
const raw = await HotSpot.deleteOne({ title: "Three Kingdoms and Four Continents cannot be Saved" (Latest addition) });
console.log(raw)
Copy the code
Mongoose summary
Before using Mongoose to operate MongoDB, there are three steps:
- Connect the mongo
- To generate the Schema
- To generate the Model
The corresponding CRUD operations can then be performed in the Model
const mongoose = require("mongoose");
const hotSpotSchema = newSchema({... })/ / generated Schmea
const HotSpot = mongoose.model("hotSpot", hotSpotSchema); / / generated Model
/ / connected mongo
mongoose
.connect("Mongo: / / 127.0.0.1:27019 / bilibili_hot", { useNewUrlParser: true })
.then(() = > console.log("MongoDB Connected"))
.catch((err) = > console.log(err));
// Database CRUD operations
HotSpot.findOne({ ... })
Copy the code
other
1. Use Docker to install MongoDB
Run a mongo container using the following command. If you don’t find the mongo image locally, Docker will automatically pull the remote image, which may take a long time.
$ docker run -p 27019:27017 --name mymongo -v ~/db:/data/db -d mongo
Copy the code
Parameter Description:
-p
: Port mapping,-p 27019:27017
It means to put inside the container27017
Port mapped to the host27019
port--name
: Container name,--name mymongo
Name the running container asmymongo
-v
: Data volume,-v ~/db:/data/db
Storing data in a container/data/db
Directory mapping to the host~/db
In the catalogue
Check the container running status. Run the docker ps command to check the container running status
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ddfc4fd875e6 mongo "Docker - entrypoint. S..."4 minutes ago Up 4 minutes 0.0.0.0:27017 / TCP, ::27019->27017/ TCP mymongoCopy the code
We can use Docker exec to go inside the container and tune MongoDB. Note that DDfc4FD875e6 is the CONTAINER ID displayed when the docker ps command is executed in the previous step. The value of the reader must be different from that of the author, so you need to replace it with your own CONTAINER ID.
$ docker exec -it ddfc4fd875e6 /bin/bash
root@ddfc4fd875e6:/#
Copy the code
Let’s check the MongoDB version. Enter mongo on the command line to enter MongoDB shell.
root@ddfc4fd875e6:/# mongo
Copy the code
Now that we’re in the MongoDB shell, we’re ready to go
# Check the MongoDB version> the version 5.0.2 ()# display database
> show dbs
admin 0.000GB
config 0.000GB
local0.000 GBCopy the code
Exit the shell and container
Enter exit and press Enter to exit MongoDB shell
> exit
bye
Type exit and press Enter to exit the container
root@ddfc4fd875e6:/# exit
exit
Copy the code
2. References
- The Mongoose’s official website
- Directing a website