mongoDB4.0 Database operation method
- 2020-12-18 01:58:31
- OfStack
mongoDB4. 0 database
Download: https: / / www. mongodb. com /
Installation:
Note: Modify the configuration file mongodb.cfg in the bin directory to delete the 'mp' field in the last line
1. Start and end the Service
net start mongodb
net stop mongodb
2. Create administrator user
mongo
use admin
db.createUser({user:"yxp",pwd:"997997",roles:["root"]})
3. Connect to mongodb using your account password
mongo -u adminUserName -p userPassword --authenticationDatabase admin
4. The database
View the database
show dbs
Switch database
use db_name
Add database
db.table1.insert({'a':1}) Creating a database ( Switch to the database to insert tables and data )
Delete database
db.dropDatabase() 删数据库(删前要切换)
Table 5.
Switch database before use
See the table
show tables 查所有的表
Increase the table
use library
db.table1.insert({'b':2}) Increase the table ( A table is created when it does not exist )
Delete table
db.table1.drop() 删表
6. Data
Increase the data
db.test.insert({ ' name':'mac'}) insert 1 article
db.user.insertMany([{},user2,user3,user4,user5]) Insert multiple
Delete the data
db.user.deleteOne({ 'age': 8 }) Delete the first 1 A match
db.user.deleteMany( {'addr.country': 'China'} ) Delete all matches
db.user.deleteMany({}) Delete all
View the data
db.user.find({'name':'alex'}) check The equivalent of where xx==xx
db.user.find({'name':{"$ne":'alex'}}) check xx!=xx
db.user.find({'_id':{'$gt':2}}) check xx>xx
db.user.find({"_id":{"$gte":2,}}) check xx>=xx
db.user.find({'_id':{'$lt':3}}) check xx<xx
db.user.find({"_id":{"$lte":2}}) check xx<=xx
To change the data
db.user.update({'_id':2},{"$set":{"name":"WXX",}}) To change the data
7.pymongo
client = pymongo.MongoClient(host=host,port=port, username=username, password=password)
db = client["db_name"] Switch database
table = db[' The name of the table ']
table.insert({}) Insert data
table.remove({}) Delete the data
table.update({'_id':2},{"$set":{"name":"WXX",}}) To change the data
table.find({}) Check the data
conclusion