Mongodb (1)

Time:2021-7-21

Let’s first understand the non relational database and relational database. First, we need to understand the relational database, such as Oracle, MySQL and so on.

Relational database and non relational database

  1. Essence: the essence of non relational database. In fact, non relational database is the castrated version of traditional relational database functions. It can greatly improve the performance of products by reducing the functions that are not used or rarely used.
  2. Price: the current non relational databases are basically free
  3. Function: in the actual development, many business requirements, in fact, do not need a complete relational database function, non relational database function is enough to use. In this case, the use of higher performance, lower cost of non relational database is of course a more sensible choice.

Introduction to mongodb:

Mongodb is a database based on distributed file storage, written by C + +. The purpose is to provide a high performance data storage scheme for web applications. It is a product between relational database and non relational database. The data structure it supports is relatively loose, which is similar to JSON’s bson format, so it can store more complex data types.

mongo shell

//After starting mongodb

var x='Hello World'
Print (x) // note that although the JS instruction is executed here, only print () can be used instead of console.log

function jspang(){
    return 'JSPang';
}
print(jspang())

Storage structure of mongodb

Mongodb (1)

In the past, the data structure of our relational database was that the top level was the library, the bottom of the library was the table, and the bottom of the table was the data. However, mongodb is different. Under the library is the collection, and under the collection is the document. You can see the following figure for an understanding.

Basic shell command

  • Show DBS: display the existing database. If you have just installed it, there will be local and Admin (config) by default. This is the default database of mongodb. We do not allow these names when creating a new database.
  • Use admin: entering the database can also be understood as using the database
  • Show Collections: displays collections in the database
  • DB: display the current location, that is, the name of the database you are currently using. This command is the most commonly used, because when you do any operation, you have to check your own database first to avoid operation errors.

Write Mongo command with JS file

Start mongodb first

// getTask.js
var username = "zjj";
var time = Date.parse(new Date());
var jsonData = {"loginUser": username, "loginTime": time}
Var DB = connect ('login ') // connect to database
db.login.insert(jsonData)

print('log print success')
Execute in this directory
mongo getTask.js

Batch injection

//Batch data insertion is performed in the form of arrays

db.test.insert([
    {"_id":1},
    {"_id":2},
    {"_id":3}
]);

//Note that the insertion should not exceed 48m at a time. Try to use static storage for. Zip and big pictures. Mongodb stores the static path, which is also a rule.

//The efficiency of array batch insertion is much higher than that of loop insertion

Update common errors

// demo01.js
var workmate1={
    name:'JSPang',
    age:33,
    sex:1,
    Job: 'front end',
    skill:{
        skillOne:'HTML+CSS',
        SkillTwo:'JavaScript',
        SkillThree:'PHP'
    },
    regeditTime:new Date()
}
 
var workmate2={
    name:'ShengLei',
    age:30,
    sex:1,
    Job: 'Java back end',
    skill:{
        skillOne:'HTML+CSS',
        SkillTwo:'J2EE',
        SkillThree:'PPT'
    },
    regeditTime:new Date()
}
 
var workmate3={
    name:'MinJie',
    age:20,
    sex:1,
    Job: 'UI design',
    skill:{
        skillOne:'PhotoShop',
        SkillTwo:'UI',
        SkillThree:'Word+Excel+PPT'
    },
    regeditTime:new Date()
}
 
var db=connect('company')
var workmateArray=[workmate1,workmate2,workmate3]
db.workmate.insert(workmateArray)
print('[SUCCESS]: The data was inserted successfully.');
//Construction data

//Run Demo01

mongo
> load('./demo01.js');
var db=connect('company')
db.workmate.update({name:'MinJie'},{sex:0})
 
print('[update]: The data was updated successfully');
//We will find that the data that needs to be modified has been greatly changed, leaving only sex: 0

Correct modification method

// demo02.js
var db=connect('company')
 
var workmate3={
    name:'MinJie',
    age:20,
    sex:0,
    Job: 'UI design',
    skill:{
        skillOne:'PhotoShop',
        SkillTwo:'UI',
        SkillThree:'Word+Excel+PPT'
    },
    regeditTime:new Date()
}
db.workmate.update({name:'MinJie'},workmate3)
 
 
print('[update]: The data was updated successfully');

But how troublesome it would be to change it every time. We usually use itupdateModifier to modify

In the next section, let’s look at the update modifier