Introduction to mongodb database

1: Installation of mongodb in windos system

2: What is mongodb

  • 1: Is a database;
  • 2: Is a non relational database, the number of documents database

3: Why learn mongodb

3.1: enterprise demand

  • Crawler development engineers are required to master this technology

3.2: reptile demand

  • If the crawler's data is a star, using mongodb is much better than using mysql, mainly because the operation of MySQL is too troublesome
  • Scratch redist: a plug-in in scratch, but its applicability is poor. Not all companies will use distributed, so there is no need to focus on learning

3.3: characteristics of mongodb

  • 1: No data structure, easy to do crawler
  • 2: High performance, with very high read and write performance
  • 3: Good support, complete documentation, good cross platform performance and high stability

4: Command of mongodb

  • Mainly introduces some common commands

4.1: basic commands

Display database

show dbs
  • effect

Clear screen

cls
  • effect

Use database / create database

use To use/Database to create
  • effect
  • Note: the database created at this time is still created in memory. There is still no data at this time. Only the data in the hard disk can be viewed using show dbs

View the current database

db
  • effect

View the set (table) of the currently used database

show collections / show tables  
  • effect

Delete database

db.dropDatabase()
  • be careful:
    • To jump to this database, you can delete the database. You can use the use command to jump to the database to be deleted
  • effect

4.2: insert data

Do not create tables manually

  • Note: when there is no table, we insert a piece of data, and the table is automatically created. When there is a table, data will be inserted into the table
db.jerry.insert({x:1})
  • effect

Create table manually

db.createCollection('name', {capped:true, size:4})
  • Parameter interpretation:

    • Name: table name
    • capped: False by default. No upper limit is set. True means there is an upper limit
    • Size: indicates the size of the set upper limit, in bytes. However, if the set capacity is less than 256 bytes, the default is 256 bytes
  • Note: if the added data exceeds the upper limit set by us, the data will be overwritten from the beginning

  • effect

  • Note: this command only creates a table. There is no data in the table. To insert data, you need to use the insert command

Check whether a table / set has an upper limit set

db.jerry1.isCapped() 
  • be careful
    • jerry: table name;
    • The return value false has no upper limit; There is an upper limit to true
  • effect

Insert data into table

db.jerry.insert({x:1})
  • effect
  • be careful
    • The inserted data can be duplicate, but the id can be used to distinguish whether it is the same data
    • When there is no such table, the table will be created first and then data will be inserted. When there is such a table, data will be directly inserted into the table

Delete table / set

db.jerry_collection.drop() 
  • effect

Insert multiple pieces of data

db.jerry2.insert([{name:'jerry',age:18,gender:'male'},{name:'juran'}])
  • be careful:
    • Remember to use []
    • Each different data is segmented using ","
  • effect

Insert multiple pieces of data using a loop (*)

  • Those with * are not required to master
for(i=3;i<10;i++)db.jerry3.insert({x:i}) 

- effect

Update existing data

  • Change existing data
db.jerry3.save({_id:ObjectId("60d08f386bd683f7c417e0ac"),name:'jerry',gengder:'male'})
  • effect

  • Note: the "_id" of the updated data must be the same

  • Insert new data

db.jerry3.save({name:'jerry',gender:'male'})
  • effect
  • Note: after specifying the id, the record will be found and updated. Otherwise, it will be inserted

4.3: query data

Query all data

db.name.find()
  • Name: the name of the table / set to query
  • effect

formatted print

db.stu.find().pretty()
  • effect

Precise vehicle query

  • In dB name. Find (), you can add data to the parentheses to realize accurate query
db.stu.find({name:'jerry'})
  • effect

Format and print accurate query data

db.stu.find({name:'jerry'}).pretty()
  • effect

Return the first data that meets the condition

  • 1: When there are no screening conditions
db.stu.findone()
  • effect
  • 2: When there are screening conditions
db.stu.findOne({age:18})
  • effect

Multi condition search

db.stu.find({age:18,homtown:'Guangzhou'})
  • effect

Returns a range of data

db.stu.find({age:{$gt:18}})
  • effect
  • operator
    • Equal: the default is equal judgment, and there is no operator
    • Less than: $lt
    • Less than or equal to: $lte
    • Greater than: $gt
    • Greater than or equal to: $gte

Returns range data for multiple conditions

db.stu.find({age:{$gt:18},hometown:'Changsha'})
  • effect

Or query

  • Syntax:
    • Or: use $or, the value is array, each element in the array is json, and Mongodb is and by default
    • db.jerry_collection.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: or:[{age:{gt:18}},{gender:false}]})
db.stu.find({$or:[{age:{$gt:18}},{gender:false}]})
  • effect

Custom query

db.stu.find({$where:function(){return this.age>18}})
  • The query age is greater than 18, which is flexible. You can change the query conditions in the function
  • effect

Range query

db.stu.find({age:{$in:[18,28]}})
  • Returns data in the age range of 18-28
  • effect

4.4: Operation query results

Returns the number of query results

  • 1: Unconditional query
db.stu.find().count()
  • effect
  • 2: Conditional query
db.stu.find({age:18}).count()
  • effect

limit and skip

  • definition:
limit Used to read the specified number of front pages n Documents
db.stu.find().limit(n)

skip Used to skip the specified quantity n Document for
db.stu.skip(n)

limit and skip When used at the same time, skip the two returned data first, and get the first two data of the skipped data
db.stu.find().skip(2).limit(2)
  • effect

mapping

  • Specifies the field to return. If it is 1, this field is returned
db.stu.find({},{age:1})
  • effect

  • be careful

    • 1: When you want to hide "_id", you need to set "_id": 0 (mapped to 0)
    • 2: There can be multiple mappings, and each mapping is divided by
    • 3: Note: the first empty curly bracket in find()

sort

db.stu.find().sort({age:-1})		# Note: age:1 is in ascending order and age:-1 is in descending order
  • effect
  • Note: if you can sort the elements that can be compared

Conditional sorting

db.stu.find({gender:true}).sort({age:-1})
  • effect

Added by iambradn on Mon, 24 Jan 2022 12:01:57 +0200