mongodb installation use and pymongo basic use tutorial

  • 2020-06-23 02:12:02
  • OfStack

(1) Installation of mongodb

After downloading tgz and extracting it, you need to add the corresponding environment variable to start mongod directly on the terminal.

The mongodb data is stored in /data/db, so the directory tree needs to be created manually. At the same time, if the permissions of mongod are not enough (it cannot write to /data/db), the permissions need to be changed by 1.


vim ~/.bashrc
export PATH="~/download/mongodb-linux-x86_64-ubuntu/bin:$PATH"
mkdir -p /data/db
ls -l /  To view data Permission for a directory to find that the owner can write 
ls -l /data To view db Permission for a directory to find that the owner can write 
chown -R "liaohuqiang" /data data Change the owner of all files under to yourself 

(2) The startup of mongod

You can then type mongod into the terminal and start mongodb, but it's running in the terminal, equivalent to a foreground process, where you can't do anything else.

You can use the --fork option, turn it on and go back to the terminal to interact and continue with your work.

fork means start mongodb as a daemon, creating 1 server process. fork is used with logpath to indicate which log file the log information is output to. logappend represents writing to the log file as an append.

Of course, there are other ways to start, not yet used, not for the moment.


mongod --fork --logappend ~/mongo.log --logappend 

(3) the use of mongo

After starting mongod, you can enter mongo at the terminal to perform relevant database operations


show dbs  Display all databases 
use doctor  Switch to a database "doctor" .   Do not switch the default use test
db.dropDatabase()  Delete database 
show collections  Display all collections 
db.sample.drop()  Delete the collection 
db.sample.insert({name:"pilgrimHui", label:"1"})  insert 1 rows 
db.sample.insert({...})  If the document already exists, it is updated _id Not being is the same insert
db.sample.find({...})  Find records that meet the criteria, simple 1 See below for some query criteria 
db.sample.find({...},{field1:1, field2:1})  Find only certain fields 
db.sample.find()  To find the sample Collection of all records 
db.sample.remove({...},1)  Delete records that meet the criteria. The parameter 1 is optional and represents deletion only 1 a 
db.sample.remove({})  delete sample Collection of all records 

(4) update operation in mongo


db.collection.update(criteria, objNew, upsert, multi)
criteria:  Query criteria, understood as sql update statements where At the back of the 
objNew:  Update operation, read as sql update statements set At the back of the 
upsert:  Insert if no queried record exists. The default false And don't insert 
multi:  Whether to update multiple. The default false , only update the rules 1 The article. 
#  For example, put all the records in the collection status Instead of 0
db.sample.update({"status": {$ne: 0} }, {$set: {"status":0}}, {multi: true})
 or 
db.sample.update({"status": {$ne: 0} }, {$set: {"status":0}}, false,true)

(4) Several query operators


$ne  Ranging from 
$gt  Is greater than 
$gte  Greater than or equal to 
$lt  Less than 
$lte  Less than or equal to 
$in and $nin  Is in the specified array 
$all  Is all in the specified array 
$or  To pair or operate on multiple keys in an array 
$slice:[1,2]  slice , Takes a number of property values (arrays) 
$size  The length of the property value (array) 
$exists:true  Select the record that exists for this field 
$not  Non of any query operator 
$regex  Use regular expression matching 

(5) Several update operators


{ $inc : { field : value } }
{ $set : { field : value } }
{ $unset : { field : 1} } 
{ $push : { field : value } }  Additional, field If an array 
{ $pull : { field : value } }  and push On the contrary 
{ $pop : { field : 1 } }  Delete the last 1 A value 
{ $pop : { field : -1 } } Delete the first 1 A value 
{ $currentDate: { <field1>: {$type:"date", ... } }  Set current time 
{ $rename: {<field1>:<newName1>,<field2>:<newName2>,...} } Rename field 

(6) User permission Settings

mongodb has no account login by default, so you need to add your account first.

The account added to the admin database is an administrator account, and the account added to other databases is a normal user account

Users can only log in to the same database as the user, including administrators.

Administrators can only manage other databases after admin has been authenticated.

6.1 Add administrator account


use admin
db.system.users.find()

db.addUser('liaohuqiang','liaohuqiang')  Add administrator user, mongodb version is different, may report an error, if the error cannot be found addUser

With this one down here


db.createUser(
 {
  user: 'liaohuqiang',
  pwd: 'liaohuqiang',
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
 }
)

6.2 Restart mongod

Restart mongod after successful creation, at which point you can open the connection with the --auth option, which represents the authentication of the user.


mongod --fork --logappend ~/mongo.log --logappend --auth

6.3 Authentication and Login

After opening, direct mongo entry operation will be limited, and login can be authorized when mongo connects. You can also reauthenticate by connecting to it.


mongo -u liaohuqiang -p --authenticationDatabase admin
mysql -h ip -u root -p  Jump in. Contrast 1 Under the mysql The connection of the 
mongo
use admin
db.auth('liaohuqiang', 'liaohuqiang')

6.4 Close connection

After the authentication login, the database can be operated as before and the connection can be closed in the admin database.


mongod --fork --logappend ~/mongo.log --logappend 
0

(7) Connection of pymongo


mongod --fork --logappend ~/mongo.log --logappend 
1

conclusion


Related articles: