Mongo uses databases

Mongod boot

Mongoimport Imports data

Nosql: non-relational database. Mongodb is one of them

1. Connect to the test database by Mongo by default (this database is undeletable and comes with mongodb installation).
2, mongodb compared with other databases, relational databases must create tables and fields, otherwise it cannot be used. However, mongodb is a “stateless mode”, the structure does not need to be designed, can be used directly, depending on what we pass into it! (Created if it does not exist)
Because the market standard is called a table, mongodb does compatible syntactic processing
Show databases: show databases/show databases
Show all collections/tables: show collections/show tables
Db/db.getName();

Database status: db.stats()

Information:Docs.mongodb.com/manual/refe…
"db" : "dnedu", : indicates the current description of database"collections": 3, : indicates how many collections there are in the current database. You can also use show Collections"objects": 14, : indicates the total number of rows in all collections of the current database, but it is only an estimate and not very accurate"avgObjSize": 60.142857142857146, : The size of each row, which is also an estimated value"dataSize": 842, : Total size of all data in the current database"storageSize": 90112 indicates the disk size occupied by the database. Bytes"numExtents": 0,: contains a count of the number of extensions in the database in all collections"indexes": 3."indexSize" : 90112,
"ok": 1.Copy the code

Console operation

Write data:

1. Common data adding mode:

Bson () is a kind of JSON storage format, short for Binary JSON. Like JSON, bson supports embedded document objects and array objects. However, BSON has some data types that JSON does not, such as Date and BinData.

db.product.insert({name:"xiaomi6",price:1999,weight:140,number:40,area:{province:"beijing",city:"beijing"}})
db.product.insert({name:"huawei01",price:2999,weight:120,number:20,area:{province:"shenzhen"}})
Copy the code

When the collection (product) does not exist, a new collection is created, and the data format is basically the same as json

2: Multi-dimensional array object add:

db.product.insert({name:"xiaomi6",price:1999,weight:140,number:40,area:{province:"beijing",city:"beijing"}})
Copy the code

Three: add array information

db.product.insert({name:"xiaomi6", price: 1999, weight: 140, number: 40, color: [' red ', 'black', 'yellow']})Copy the code

Mongoimport --db dnedu --collection product --drop --file Dr. Json -- mongoimport --db dnedu --collection product --drop --file Dr. Json -- mongoimport --db dnedu --collection product --drop --file Dr. Json -- mongoimport --db dnedu --collection product --drop --file Dr. Json It is also possible to import directly without dropping --drop

Data query:

General query:

Db.product.find ()

Db.product.findone ();

Query: db.product.find({name:"huawei01"}). Mysql > select * from table name where name = "huawei01"

ID: The value of the ID field is obtained by the mongodb algorithm. The value information corresponding to the ID is unique in the world, which is equivalent to the primary key ID in Mysql. The ID can be set by itself, but it is not recommended

Range queries
Key words:$gt   $lt   $gte   $lte
Copy the code
Mysql mysql mysql mysql mysql mysql mysql mysql mysql mysql mysql mysql mysql mysql mysql mysql mysql
Db.product. find({price:{price:{price:{'$gt': 2500}})Copy the code

Db.product. find({price:{price:{db.product.find({price:{price:{price:{'$lt': 2500}})Copy the code

Set multiple query conditions:
Db.product. find({price:{price:{db.product.find({price:{price:{'$gt':2500},weight:{'$lt': 100}})Copy the code

Db.product. find({price:{price:{db.product.find({price:{price:{'$gt':2500},weight:20})
Copy the code

Multidimensional field query:
Query area for changsha db.product.find({baf db.product.find(baf db.product.find(baf db.product.find))'area.province':"changsha"})
Copy the code

Array conditional query:
Db.product. find({color: 'red'});Copy the code

Db.product. find({color:{' db.product.find({color:{'$all': [' red', 'black']}})Copy the code

$or ($or)
Db.product.find ({price = 3000 or number = 100 db.product.find({price = 3000 or number = 100)'$or':[{price:3000},{number:{'$lt': 100]}}})Copy the code

Restricted query fields:
Db.product. find({price:3000},{name:1}) db.product.find({price:3000},{name:1,_id:0}) This statement really only has the name fieldCopy the code

1. The field is displayed in the query. 0

If you want, for example, name to be 1 and price to be 0, then you will get an error because mongodb rules (can be 1 for both pages and 0 for all pages) will either output all or none, except for _id, which can be set to 0,1.

db.product.find({price:3000},{name:1,price:0,_id:0})
Copy the code

Modify data:
Key words:$setDb.product. update({name:"huawei06"}, {'$set':{name:"update before name"}});
Copy the code

If there is no $set in the statement, it will delete all the other fields except id, and then keep the fields we set ourselves. The fields in the black graph below will become the fields left in the image below

Db.product. update({name:"huawei07"}, {history:"i don't know"})
Copy the code

The above statement removes all the other fields except id, and then creates the hisotry field

Delete data: Delete all data whose name is equal to huawei03

db.product.remove({name:"huawei03"})

Remove fields: db. The product. The update ({name: "huawei04"}, {' $unset: {name: 1}}); , only one entry will be deleted

Fuzzy matching:

Db.teacher.find ({name:/ ha /}). Limit (1) FindOne: db.teacher.findone ({name:/ ha /})Copy the code