Start MongoDB server: sudo service mongod start or Sudo/usr/local/mongodb/bin/mongod--config/etc/mongodb.conf
Stop MongoDB service side: sudo service MongoDB stop
Querying the MONGODB Server Status command query status
Ps-ajx|grep Mongod or sudo systemctl status MongoDB
Database operations
Access database: MONGO
Show all databases: show databases->show DBS
View Player a database: DB
Database switchover (selection and creation): Use database name Ps:use test the database does not appear by default unless the collection is created
View current database name: DB
Display collection: Show Collections PS: Here's a collection of data in MySQL table
View data for the collection: DB. Collection name. Find ({}) Ps:db.student.find ({}) where {} can not be written
Delete database: Db.dropdatabase ()
Delete databases in SQL statements: drop database test3;
Exit Database: Ctrl + C or exit
Collection (table) operations
Create a collection in a database: Db.createcollection (name, options)
Name is the names of the collections to be created
Options is a document that specifies the configuration of the collection, option?? The parameter is optional, so only the specified collection name is required.
PS: Create a collection with the name Students:db.createCollection ("students")
View all collections of the current database: Show collections or Show tables
Delete collection: Db.students.drop ()
Basic Data operations
Insert Data :d B. Collection name. When insert document is inserted, MongoDB assigns a unique objectid to the document if the _ID parameter is not specified
Insert, if present will error
Ps:db.students.insert ({"Name": "GJ", gender:1})
View data: Db.students.find ({}) or Db.students.find ()
You can also specify the ID yourself:
Ps:db.students.insert ({id: "20180418", "name": "GJ", gender:1})
Simple query
Query all data: Db.students.find () or Db.students.find ({})
Update data:
Db. Collection name. Update (<query>,<update>,{upsert: <boolean>,multi: <boolean>})
Argument query: Query condition, similar to the where part of SQL statement update
Parameter update: update operator, similar to the set part of SQL statement update
Parameter upsert: Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.
Parameter multi: optional, default is False, indicates that only the first record found is updated, and a value of TRUE indicates that all documents that meet the criteria are updated
PS: The default value updates one data and the structure is modified db.student.update ({},{name: ' MNC '})
The default value updates one data, modifies only the fields, does not break the data db.stu.update ({},{$set: {name: ' MNC '}})
Full-text file find update find the first, the {name: ' hr '} corresponding to the change to {name: ' MNC '}, all other fields are discarded
Db.students.update ({name: "hr"},{name: "MNC", gender:1})
Specifies the property update, through the operator $set data structure will not be destroyed, but also only modify a
Db.students.update ({"Name": "MNC"},{$set: {gender:0}})
Modify multiple matches to the data: several operations must cooperate with $set, otherwise it will error note true and flase is lowercase
PS: Change all genders to girls db.students.update ({},{$set: {gender:0}},{multi:true})
Modify all GJ to "Guo Jing" and do not destroy the structure db.students.update ({"Name": "GJ"},{$set: {"name": "Guo Jing"}},{multi:true})
"$set" can also be quoted: Db.students.update ({"Name": "GJ"},{"$set": {"name": "Guo Jing"}},{"Multi": true})
Method FindOne (): Query, return only the first document: DB. Collection name. FindOne ({conditional document})
Method Pretty (): Formats the result: DB. Collection name. Find ({conditional document}). Pretty ()
Save:
db. Collection name. Save If the document's _id already exists, and the structure changes, if the document's _id does not exist then add note, if _id is written as ID, the ID will be the key
Ps:db.students.save ({"_id": "20180418", "name": "Zhang San", "Gender": 1})
Delete:db. Collection name. Remove (<query>,{justone: <boolean>})
parameter query: Optional, the condition of the deleted document
Parameter justone: Optional, if set to TRUE or 1, delete only one, default false, means delete multiple
PS: Delete only the first match to Db.students.remove ({"Name": "GJ"},{"Justone": true})
Delete all: db.students.remove ({})
A sample of Max
PS: Create a fixed-length set called sub with a length of 5 bytes and a number of documents that can hold 3
Db.createcollection ("Sub", {"capped": True,size:5,max:3}) Here the previous document is automatically replaced when the number of documents inserted exceeds 3
Comparison operators
equals, default is equal to judgment, no operator
Less than $LT computer term inside is less than, meaning is smaller than, abbreviation is LT
Less than or equal to $lte
Greater than $gt corresponds to greater than, meaning is greater than, abbreviation is GT;
Greater than or equal to $gte
Not equal to $ne corresponds not equal to, meaning is not equals, abbreviation NE;
PS: Db.sub.find ({"Count": {"$gte": 2}) for subjects with a query period greater than or equal to 2
Find title is not MongoDB's subject Db.sub.find ({' title ': {' $ne ': ' MongoDB '}})
logical operators
Queries can have multiple conditions, and multiple conditions need to be connected by logical operators
Logic and: The default is the relationship between logic and multiple conditions directly connected with commas
PS: the subject Db.sub.find ({"Count": {"$gte": Ten}, "title": "Web"}) with the title of the Web, with a query period greater than or equal to 10
Logic or: Using $or
PS: Query class greater than 10, or subject to the Web Db.sub.find ({"$or": [{"Count": {"$gt": 10}},{"title": "Web"}]})
Range operator--"$in": Use "$in", "$nin" to determine if it is within a range
PS: 8, 14 subject Db.sub.find ({"Count": {"$in": [8,14]}})
Inquiry class not 8, 14 subject Db.sub.find ({"Count": {"$nin": [8,14]}})
Limit vs. Skip:
Db. Collection name. Find (). Limit (number), which indicates how many documents to get, if no parameters are specified or 0 displays all documents in the collection
Method Limit (): Used to read a specified number of documents PS: Query 2 information db.sub.find ({}). Limit (2)
method Skip (): Used to skip a specified number of documents
db. Collection name. Find (). Skip (number) Parameters number indicates skipped record count, default value is 0
PS: Inquiry for student information starting from article 3 db.stu.find (). Skip (2)
Limit () and Skip () are used together:
PS: Starting with the second article, and as long as three db.sub.find (). Skip (2). Limit (3)
MongoDB interacts with python :
To create a client:
#No security certifications:Client = Mongoclient ('localhost', 27017) Client= Mongoclient ('mongodb://localhost:27017/Database name') Client= Mongoclient ('Mongodb://localhost:27017/test1')#with safety certifications:Client = Mongoclient ('mongodb://user name: password @localhost:27017/database name') Client= Mongoclient ('Mongodb://t1:[email Protected]:27017/test1')
Code implementation:
fromPymongoImportmongoclientclient= Mongoclient ("Mongodb://localhost:27017/test1")#get a database named Test1test1= client['test1']#can also be written test1 = Client.test1#get a collection of names called T1T1= test1['T1']#can also be written as T1 = Test1.t1#the data to insertS1 = {"name":"Guo Jing"," Age": 18}s= T1.insert_one (S1)
Update data:
s = T1.update_one ({"name":" Guo Jing "},{"$set ": {"name":" Huang Rong "}})
Delete data:
S1 = T1.delete_one ({'name':' Huang Pharmacist '})
Find data:
S1 = T1.find_one ({'name':' Huang Rong '})
# Find all data t1.find ()
Database of MongoDB