1. Start mongoDB
mongod –dbpath E:\mongoDB\data\db
<img src="https://img.php1.cn/3cd4a/ 9b0d/ae9/2d998ad7838fbf16.jpeg
Open another command line ,cd to the bin directory of the mongodb directory,
Enter mongo
Start the database operation interface
<img src="https://img.php1.cn/3cd4a/9b0d/ae9/2d998ad7838fbf16.jpeg
2. Operation database commands
①Common database commands
show dbs displays all databases
use abc switches to abc database
db.dropDatabase() deletes abc database
②Create database
Use student database
use student (if the database does not exist, it will be created)
In student Create the user table under the database
db.createCollection(“user”)
Query all table names under the current database
db.getCollectionNames() (query all collection)
③Insert data into the collection
db.user.insert([{name:”李四”,age:4},{name:”王五” ,age:2}])
db.user.insert({name: “Zhang San”,age:20})
④Modify data
db.user.update({name:”张三2″},{KaTeX parse error: Expected 'EOF', got '}' at position 13: set:{age: 20}}̲) db.user.u…set:{age:3}})
Modify multiple pieces of data
db.user.updateMany({age:3}, {
set:{age:1}})
⑤Delete data
db.user.remove({age:3})
db.user.remove({})
⑥Query operation
db .user.find()
db.user.find({name:”张三”})
Query those whose name is Zhang San and whose age is less than 10 years old
db.user.find({name:”Zhang San”,age:{$gt:10}})
//Only want the name and age columns to appear,and _id does not appear
db.user.find({},{name:1,age:1,_id:0})
//Limit the number of entries
db.user.find({}).limit(3)
//How many displays to skip
db.user.find({}).skip( 2).limit(2)
db.user.find({}).limit(2).skip(2)
//Sort 1 ascending order-1 descending order
db.user.find({}).sort({age:-1})
//If the sorting is written together with the limit number of items ,the number of skipped items& #xff0c;Then perform sorting first,and then perform the number display operation
Query those whose name is Zhang San and whose age is less than 10 years old
db.user.find({ name:”Zhang San”,age:{$gt:10}})
//Only hope that the name and age columns will appear,and _id will not appear
db.user.find({},{name:1,age:1,_id:0})
//Limit the number of entries
db.user.find({ }).limit(3)
//How many displays to skip
db.user.find({}).skip(2).limit(2)
db.user.find({}).limit(2).skip(2)
//Sort 1 ascending order-1 descending order
db.user.find ({}).sort({age:-1})
//If sorting is written together with the limit number of items ,the number of skipped items,, the sorting is performed first, ;Perform item number display operation
⑦Delete collection (student > user)
use student
db.user.drop() //Data below If all collections are gone,the database will be automatically deleted
Export the mongdb database data or import the json file into the database
//Insert the json file into the database
mongoimport –db leishen –collection goods –file E:\Vue-P\sql-leishen\goods.json –type json
//Export mongodb data into json
mongoexport -d database name -c collections name -o filename.json/filename.csv -f field
mongoexport -d leishen -c user -o user.json
Parameter description:
-d database name (database)
-c collections name
-o file name to be saved (xxx .json/xxx.csv)
-f outputs specific attribute columns (generally, this parameter can be omitted)