[MongoDB] database installation and basic operation - mongoose - addition, deletion, modification and query

Posted by Alicia on Sun, 23 Jan 2022 18:28:26 +0100

1. Introduction



NoSQL refers to a non relational database. NoSQL is sometimes referred to as the abbreviation of Not Only SQL. It is a general term for database management systems different from traditional relational databases.

NoSQL is used to store very large-scale data. (for example, Google or Facebook collects trillions of bits of data for their users every day). These types of data storage do not need fixed patterns and can be expanded horizontally without redundant operations.


MongoDB is a database based on distributed file storage. Written in C + +. It aims to provide scalable high-performance data storage solutions for WEB applications.

MongoDB is a product between relational database and non relational database. It is the most functional and relational database among non relational databases.

RDBMS vs NoSQL

RDBMS

  • Highly organized structured data
  • Structured query language (SQL)
  • Data and relationships are stored in separate tables.
  • Data manipulation language
  • Strict consistency
  • Basic transaction

NoSQL

  • Represents more than just SQL
  • No declarative query language
  • There are no predefined patterns
    -Key value pair storage, column storage, document storage, graphics database
  • Final consistency, not ACID attribute
  • Unstructured and unpredictable data
  • CAP theorem
  • High performance, high availability and scalability

Pros / cons of NoSQL

advantage:

  • high scalability
  • Distributed computing
  • low cost
  • Flexibility of architecture, semi-structured data
  • There is no complex relationship

Disadvantages:

  • No standardization
  • Limited query capabilities (so far)
  • Final consistency is not an intuitive procedure

Basic concepts

  • Database database
  • collection (array)
    Similar to the data table in SQL, it is essentially an array containing multiple document objects, [{}, {}, {}]
  • Document object document
    Similar to records in SQL, a document object {} is a record

A [database] is composed of multiple [sets], and a [set] contains multiple [document objects]

Documentation manual

Official website
https://www.mongodb.com/
Official website manual
https://docs.mongodb.com/manual/
Rookie tutorial
https://www.runoob.com/mongodb/mongodb-tutorial.html

2. Installation & startup

Download and install
https://www.mongodb.com/try/download/community

The current version is 4.4.6

Add to system environment variable after installation

Create a db folder in the data folder of drive C

Enter mongod in cmd

Open another CMD belonging to mongo

To sum up

  1. Start mongodb server: mongod
  2. Modify the default port: mongod --port new port number
  3. mongodb default port: 27017
  4. Set the storage path of mongodb database: mongod --dbpath path
  5. Connect to mongodb database: mongo

You can use MongoDB Compass graphically
Download address https://www.mongodb.com/try/download/compass

3. Use

3.1 basic use

show dbs or show databases
View all databases
use xxx
Switch to the specified database
db
View the database of the current operation
show collections
View all collections in the current database

3.2 CRUD operation of database

Insert data insert

Insert a piece of data

db.collectionName.insertOne( {name:'liu'} )

db represents the database of the current operation
collectionName represents the collection of operations. If not, it will be created automatically

If the inserted document is not provided manually_ id attribute, an

Insert multiple pieces of data

db.collectionName.insertMany( [ {name:'ykyk'} , {name:'ykyk123'} ] )

It needs to be wrapped in [array]

Universal API [one or more can be inserted]:

db.collectionName.insert()



Visual pages are also very convenient to use

Query data find

db.collectionName.find()

Query all documents in the collection, that is, all data.
The whole array object is queried. There is an object wrapped in the outermost layer.

Condition query

db.collectionName.find({name:"hhh"})


The result is an array, which can be directly followed by the index subscript

find first

db.collectionName.findOne() 

The returned object is the first object in the queried object array (the returned object is the object)

Number of query documents
db.collectionName.count() or dB collectionName. Length() counts the number of documents

be careful:

> db.students.find({_id:222}).name  //error
> db.students.findOne({_id:222}).name //correct
  1. mongodb supports querying directly through the attribute values of embedded documents
{
	name:'ykyk',
	hobby:{
		movies:['movie1','movie2'],
		cities:['zhuhai','chengdu']
	}
}

db.users.find({hobby.movies:'movie1'}) //error
db.users.find({"hobby.movies":'movie1'})//In this case, the attribute name of the query must be enclosed in quotation marks
  1. Use of query operators

Comparison operator
$gt greater than
$gte is greater than or equal to
$lt less than
$lte less than or equal to
$ne is not equal to
Another way to write $eq equals
$or or

// Greater than 200
db.users.find({num:{$gt:200}})
// Greater than 200 and less than 300
db.users.find({num:{$gt:200,$lt:300}}) 

//Greater than 300 or less than 200
db.users.find(
    {
        $or:[
            {num:{$gt:300}},
            {num:{$lt:200}}
        ]
    }
)  
  1. Paging query
db.users.find().skip(Page number-1 * Number of items displayed per page).limit(Number of items displayed per page)
db.users.find().limit(10) // Top 10 data
db.users.find().skip(50).limit(10) // Skip the first 50 data items, that is, the data items 61-70 are queried, that is, the data on page 6
  1. Sort: find query results are sorted by id in ascending order by default
// 1 indicates ascending order and - 1 indicates descending order
db.emp.find().sort({sal:1}) 
// First, sort in ascending order according to sal. If the same SAL is encountered, sort in descending order according to empno
db.emp.find().sort({sal:1,empno:-1}) 

Note: skip, limit and sort can be called in any order. The final result is to tune sort first, then to skip, and finally to limit.

Set the projection of query results, that is, only the fields you want are filtered out

// Only the ename field is displayed in the matched document
db.emp.find({},{ename:1,_id:0}) 

Query through visual interface

Modify data update

Replace entire document

db.collectionName.update(condiction,newDocument)

To modify the corresponding attribute, you need to use modification operators, such as $set,$unset,$push,$addToSet

db.collectionName.update(
	// query criteria
	{_id:222},
	{
		// Modify corresponding properties
		$set:{ 
			name:'ykky',
			age:21
		}
		// Delete the corresponding [attribute]
		$unset:{
			gender:1 //1 here can be changed to other values without any influence
		}
		
	}
)

update is equivalent to updateOne() by default, that is, only the first of the matched documents is changed
updateMany() can be used to change all documents that match

db.students.updateMany(
	{name:'ykky'},
	{
		$set:{
			age:21,
			gender:222
		}
	}
)

Adding data to an array

db.users.update({username:'yk'},{$push:{"hobby.movies":'movie4'}})

// If the data already exists, it will not be added
db.users.update({username:'yk'},{$addToSet:{"hobby.movies":'movie4'}})

Self increasing and self decreasing operator $inc

// Increase num by 100
{$inc:{num:100}} 
// Let num subtract 100 from itself
{$inc:{num:-100}} 
// Increase the salary of employees whose salary is less than 1000 by 400
db.emp.updateMany({sal:{$lt:1000}},{$inc:{sal:400}}) 

Delete data remove

[generally not deleted]

db.collectionName.remove() 

remove deletes all matching documents by default. Equivalent to deleteMany()
remove can add a second parameter to delete only the first matching document. This is equivalent to deleteOne()

db.students.remove({name:'ykky',true})
db.collectionName.deleteOne()
db.collectionName.deleteMany()

Delete all data

db.students.remove({})

Poor personality, internal documents are deleted one by one. Delete the entire collection to improve efficiency.

Delete collection

db.students.drop()

Delete database

db.dropDatabase()

Note: to delete the attributes of a document, you should use update.
The remove and delete series delete the entire document

When the deleted condition is an embedded attribute:

db.users.remove({"hobby.movies":'movie3'})

4. Relationship between documents

one to one

Reflect one-to-one relationship through embedded documents

one to many / many to one

User - order
Each order data uses an attribute to save the user's id

db.users.insert([
	{_id:100,username:'yk'},
	{_id:101,username:'ykyk'}
])

db.orders.insert([
	{list:['apple','banana'],user_id:100},
	{list:['apple','banana2'],user_id:100},
	{list:['apple'],user_id:101}
])

Query yk all orders:

① First, get the id of yk

var user_id=db.users.findOne({name:'yk'})._id;

② Query the corresponding order from the order set according to the id

db.orders.find({user_id:user_id})

many to many

[commodity] - [classification]
Each commodity data uses one attribute to store the IDs of multiple classifications
Each classification data uses an attribute to store the IDs of multiple commodities
[teacher] - [student]

db.teachers.insert([
    {
        _id:100,
        name:'yk'
    },
    {
        _id:101,
        name:'ykyk'
    },
    {
    	_id:102,
    	name:'ykky'
    }
])

db.students.insert([
	{
		_id:1000,
		name:'jun',
		tech_ids:[100,101]
	},
	{
		_id:1001,
		name:'jun2',
		tech_ids:[102]
	}
])

5. mongoose

Chinese documents
http://www.mongoosejs.net/
It is recommended to read the following English documents directly
https://mongoosejs.com/docs/guide.html

5.1 introduction

  1. mongoose is a js Library in nodejs dedicated to operating mongodb databases

  1. Objects in mongoose:

Schema schema object (used to constrain the structure of a document)
Model model objects (i.e. collections in mongodb)
Document object (i.e. document in mongodb)

5.2 installation

npm i mongoose

5.3 connecting and disconnecting databases

// 1. Introduce mongoose
const mongoose = require("mongoose");

// 2. Connect to mongodb database
mongoose.connect("mongodb://localhost/admin", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

// 3. Monitor the connection status of mongodb database
// Event triggered after binding database connection success
mongoose.connection.once("open", () => {
  console.log("Database connection succeeded!");
});
// Binding database connection failure event
mongoose.connection.once("close", () => {
  console.log("Database disconnected");
});

// 4. Disconnect the database (generally not used) [generally only connect once and will not be disconnected unless manually disconnected]
mongoose.disconnect();

5.4 creating Schema objects and Model objects

const mongoose = require("mongoose");

mongoose.connect("mongodb://localhost/admin", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

mongoose.connection.once("open", () => {
  console.log("Database connection succeeded!");
});

const Schema = mongoose.Schema;
// Create a schema object
const stuSchema = new Schema({
  name: String,
  age: Number,
  gender: {
    type: String,
    default: "female",
  },
  address: String,
});

// Create a model object from a Schema
// Model represents the collection in the database. Only through model can the database be operated
// mongoose.model(modelName, schema)
const StuModel = mongoose.model("student", stuSchema);

5.5 add, delete, query and modify the Model object

Add action create

Create one or more documents and add them to the database

Model.create(doc(s), [callback])

parameter

  • doc(s) can be a document object or an array of document objects
  • Callback callback function that is called when the operation is completed.
// Insert a document into the database
// StuModel.create(doc, err=>{})
StuModel.create(
  {
    name: "YK",
    age: 18,
    gender: "male",
    address: "WuHu",
  },
  (err) => {
    if (!err) {
      console.log("Insert succeeded!");
    }
  }
);



mongoose automatically pluralizes the set name

UserModel.create({ user_id: 100, name: "ykyk" }, function (err) {
  if (!err) hui{
    console.log("Insert successful");
  } else {
    console.log(err);
  }
});

let data = [
  { user_id: 101, name: "yk1", age: 19 },
  { user_id: 102, name: "yk2" },
];
UserModel.create(data, function (err) {
  console.log(arguments[1]); //The second value represents the added document object, which is an array
});

Query operation find

Model.find(conditions,[projection],[options],callback)
Model.findOne(conditions,[projection],[options],callback)
Model.findById(conditions,[projection],[options],callback)

parameter

  • Conditions: query conditions
  • Projection: projection {name: 1, gender: 1, _id: 0} or 'Name Gender-_ id'
  • Options: Query options {skip: xx, limit: xx}
  • Callback: the query result will be returned through the callback function, which returns an array
UserModel.find({}, function (err, data) {
  console.log(data);
});

UserModel.find(
  { name: /yk/i },
  "name gender -_id",
  { skip: 2, limit: 1 },
  function (err, data) {
    console.log(data); //Returns an array of document objects
  }
);

findById returns the Document object [Document object is an instance of Model]

findById
UserModel.findById("5f9fbfba14319e492c0f5bc4", function (err, data) {
  console.log(data);
  console.log(data instanceof UserModel); //The document object returned by true belongs to the instance object of the model object (i.e. Collection)
});

Number of documents queried

UserModel.count({}, function (err, count) {
  console.log(count);
});

Modify operation update

Model.update(conditions,[doc],[options],callback)
Model.updateMany(conditions,[doc],[options],callback)
Model.updateOne(conditions,[doc],[options],callback)
Model.replaceOne(conditions,[doc],[options],callback)

parameter

  • conditions query criteria
  • doc modified document object
  • options configuration parameter {multi: true}
  • Callback callback function
UserModel.updateOne({ name: "yk" }, { $set: { age: 22 } }, (err,data) => {
  if (!err) {
    console.log("Modified successfully");
  }
});

Delete operationremove

model.remove(conditions,callback)
model.deleteOne(conditions,callback)
model.deleteMany(conditions,callback)
UserModel.remove(
  {
    name: "yk",
  },
  (err, data) => {
    console.log("Delete succeeded");
  }
);

remarks

Document corresponds to the collection document one by one. Document is an instance of Model
All the results queried through the Model are documents

You can also manipulate the database through document

const stu = new StuModel({
	name: 'ykykyk',
	age: 19
})
console.log(stu)
stu.save()

StuModel.findOne({}, (err, doc) => {
	if(!err){
		doc.update({$set:{age:22}}, err => {
			if(!err) {
				console.log('Modified successfully');
			}
		}
	}
	// There are many other ways
	// get()
	// set()
	// toObject()
});


5.6 modular processing

  1. Create a separate database connection file dbconncet js
const mongooes = require("mongoose");

mongooes.connect("mongodb://localhost/mongooes_test", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

mongooes.connection.once("open", function () {
  console.log("Connection succeeded");
});
  1. Create a model object file for each collection js
const mongooes = require("mongoose");
const Schema = mongooes.Schema;

const userSchema = new Schema({
  user_id: String,
  name: String,
  age: Number,
  gender: {
    type: Number,
    default: 0,
  },
});

// Define model
const UserModel = mongooes.model("user", userSchema);

module.exports = UserModel;
  1. In the final file index JS to introduce database connection files and create model files
const mongooes = require("./dbconncet");
const PostModel = require("./models/postModel");

PostModel.findOne({}, function (err, docs) {
  if (!err) {
    console.log(docs);
  }
});