1024programmer Java Open the mongodb database command line_Detailed interpretation of mongoDB basic commands

Open the mongodb database command line_Detailed interpretation of mongoDB basic commands

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}, {

math?formula=set%3A%7Bage%3A1%7D%7D)%2F%2F%E4%BF%AE%E6%94%B9%E5%A4%9A%E6%9D%A1%E6  %95%B0%E6%8D%AEdb.user.updateMany(%7Bage%3A3%7D%2C%7Bset:{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&#xff0c ;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)

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/736974

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索