I. Initial knowledge of MONGODB
MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for WEB applications.
MongoDB is a product between a relational database and a non-relational database, and is the most versatile and most like relational database in a non-relational database.
The biggest difference between it and the relational database we use is the constraint, it can be said that the file type database is almost non-binding, theoretically there is no primary foreign KEY constraint, no stored data type constraints, etc.
There is a "table" concept in the relational database, with the concept of "field" and the concept of "data entry".
MongoDB also has the above concept, but the name has changed some, strictly speaking, the concept of the two is similar, but there are some differences, but it does not matter, we should be the above concept is good
Light said, or a little vague, let's do a small example, make a comparison
Let's make a table below:
This is a very simple user table that we made with the relational database, right?
Let's look at the data structure of MongoDB:
This...... Is this a list of three dictionaries? You're right, if you understand the list and the dictionary, then it proves that you only Python, in other languages it is another type, we put this type of thing, called: Json
Each piece of data stored in each of the MongoDB tables (Collection) is a single field in each of the Json,json (Key) We call it: Field
In this context, we draw three keywords, collection is the concept of "table" in relational database, documents is "Data entry", field is "fields"
Two. MongoDB Installation Deployment
About the installation of MongoDB
First share to you a MongoDB 3.4 MSI installation package Click here to download
After the installation is complete, start to enter the configuration link, first we want to enter the directory:
"C:\Program Files\mongodb\server\3.4\bin"
Configuring Environment variables
After the operation, Windows 7 is not quite the same as Windows 10, similar
"OK" in the end of Windows 101 Road
What Windows 7 needs to note here is that the path path is moved to the end, enter: "; C:\Program Files\mongodb\server\3.4\bin "must be taken."
ok! here, we've basically finished some operations.
Let's try the results.
Open cmd window
Then let's create a directory of "C:\data\db\"
Okay, the catalogue is already there, try again.
All right, it's open.
Then the service opened, how to connect the client, then we need another CMD window to open the MONGO client
In this case, we have successfully completed the deployment of the server, as well as a link to the client.
Tip: Don't turn off these two cmd windows, or you'll have to reopen them.
Three MongoDB basic operations one creates libraries and tables
Create a database: Here, as with the general relational database, you must first create your own database space
Yes, MongoDB design is more casual, no think you are creating, use luffycity_com is not there, so MongoDB think you are to create and using
This concept must be clear oh, mongodb if you use a non-existent object, then you are creating this object
Using an object that does not exist, it represents the creation of the object, and we use this fallacy to create a table (Collection) to try
It does not seem to be a fallacy to really succeed in creating a Oldboy collection
So the next step is to add a piece of data to the table (Collection), how do I add it?
Two. MongoDB Insert Data
Insert : inserting one or more data, need to be allowed to insert a number of parameters, this method is now officially not recommended
Db. Oldboy.insert ({"Name": "Dragonfire", "Age": 20})
insertone: Inserting a piece of data, official recommendation
We can see that the return values of the two methods are very different.
insertmany: inserting multiple data, no parameter control, official recommendation
This is where we inserted multiple data into the Luffycity_com.oldboy:
[{ "name":"dragonfire""Age":
{
"Name": "Wusir",
"Age": 19
}]
Here to leave a data type of suspense
Insert completion will be queried
Three. MongoDB Query data
This is not a select in MySQL.
Find () Unconditional lookup: Returns all data in this table (Collection) at once
Db. Oldboy.find ({name: "WUSIR2B"}) Condition Lookup: Name equals WUSIR2B data, this returns multiple results
"_id": Objectid is what we inserted when there is no field and there is no _id this,
This is mongodb automatically adds to our system unique identifier "_ID" is a objectid type
FindOne () unconditionally finds a data, default the first data in the current collection
FindOne ({age:19}): Condition finds data with age equal to 19, if more than one data is returned to the previous data
Query data, found some data errors, to modify what to do?
Four. MongoDB's modified data
Update ({"Name": "Dragonfire"},{$set: {"Age": 21}}): Modifies the contents of the data according to the conditions, as with insert, does not recommend the wording of the update
To change the name to 21 in Dragonfire, note that ({"condition"},{"keyword": {"Modify Content"}}), where if the condition is null, all data in the collection will be modified
Updateone ({"Age": 19},{$set: {"name": "Wssb"}}): Modifies the contents of a piece of data according to the conditions, such as multiple bars, only the highest previous data is modified
Change the name of the first data in all data with age equal to 19 to WSSB
Updatemany ({"Age": 19},{$set: {"name": "Pig_qi"}}): Modify the contents of all data according to the conditions, multiple modifications
Change the name of all data in age equal to 19 to WSSB
So we said before that the flexibility of MongoDB, does not mean that I want to create, so that if the article does not have a name attribute, he will automatically create a Name property and assign the value "WSSB"
Changed for half a day, I think, I do not want the data, how to do it?
Five. MongoDB Delete data
Remove ({}): delete Data unconditionally, here to be aware, this is to delete all data, empty collection
Remove ({"Name": "Dragonfire"}) : Condition Delete all document with name equal to "Dragonfire"
Then let's talk about the MongoDB data type, tell you what the MongoDB data type
MongoDB one to increase the deletion of the search