Start: 1) Create a folder to store MongoDB's data when you start specifying this folder to store the MongoDB storage path my directory is D:\data
2) Start MongoDB service into the bin directory where MongoDB is installed enter Mongod--dbpath=path here is the path to the stored data you just created
If the port is not added by default port is 27017 You can also specify a specific port for your MongoDB service through the following instructions
Mongod--path Path--port port
At this point our MongoDB service port has been started in 27000, this window has been opened
3) Connect to the database we need to reopen a console to connect to the database by using the following command MONGO Ip:port
Since we are connected to our native database service does not need to enter the IP as above we configured the MongoDB service to start at Port 27000 so we are going to connect the database with the following instruction
MONGO--port 27000
This allows us to connect to the database (we need IP when we need to connect to a remote database)
You might want to think about setting the path every time you start a service. It's annoying and we don't have any settings for MongoDB when we start it. The word is the default configuration file.
Read configuration file Startup
1) First you need to create this configuration file I am under D:\data mongodb.conf (different data directories configure different conf files to implement different configurations)
2) Write Conf You can view the properties that you can configure by Mongod--help the properties are configured by key-value pairs
Here is the contents of my configuration file
Dbpath=D:\data //Data store path LogPath=D:\data\log\log.log//path to log file storage logappend=/ / The way the log file is added adds up to this no parameter value so null journal= //start log no parameter value is empty
port=27000//port number to start
3) Read config file start MongoDB service mongo-f args args is the location of your configuration file
You will find this way to start, and how nothing happens in the console is a cursor in fact, I thought it didn't start. At this point, you open the directory where we configured the log file, and we find the log message.
Path port number 27000 through the log file discovery is indeed a MongoDB service that is launched through our configuration file
3) Connect the database with the command to start the console so we're connected to our database.
Basic operations
First, we'll start with some basic instructions about the database.
Db.version () This is the version information for the output database
Db.stats () The state of the database
The above can be seen that my MongoDB version is 2.6.5 because the default connection is the test database display there are three collection
Db\db.getname () View the name of the database currently in use
Show DBS View all databases under your storage path
Db.dropdatabase () Delete the currently used database
The example above shows a view of the currently used database name display test again through show DBS view all databases under data at this time our connection is still on test via db.dropdatabase () delete test database on show DBS Discovery T The EST database was actually deleted by us.
Next we demonstrate the process of re-creating the test database and adding a collection to test
Use databasename using the database This command will create this database when you do not have the databasename you specified in the database list (note that this database does not exist at this time only if you actually inserted a collection after the database exists)
Db.createcollection (name,{capped:<boolean>,autoindexid:<boolean>,size:<number>,max:< Number>})
Capped whether the set limit is turned on defaults to False
Autoindexid whether to use _id as the index defaults to True _id is the GUID that the database adds for us in order to guarantee the uniqueness of the data
Size of storage space
Max Maximum storage entries
(The settings of the capped parameter can be ignored and will be error when capped and Max are used together)
If we want to create the default collection can be directly db.createcollection ("name") through show collections to view our created collection Db.collenctionname.drop () Delete collection
In the example above we demonstrated the creation of collection view collection Delete Create collection (in the example above we can insert data directly into a nonexistent collection when we do not need to set any settings on the collection) MongoDB will automatically create a collection for us
Inserting data into a collection (tricks)
Db.insert ({key:value,key:value})
The above example is simply like the front end because MongoDB itself stores the stored document as a key value (and different documents of the same collection can be completely different structures)
Db.collectionname.save () (Will later write update save is actually a collection of Save and update)
Loop Insert (I'll first delete the data inserted above)
Db.collectionname.update (<query>,<update>,<upsert>,<multi>) modify a document that already exists
Matching Criteria for <query> queries
<update> Updated document content (just add the contents of your updated document here, if you want to implement a upsert is to add a document when there is no document, add all the parts of the document here)
<upsert> document or Boolean defaults to False when the document we want to update does not exist in MongoDB, adding a document without action or setting it to true will add a document to us when the document is not found
The above example when we do not set up <upsert> when the document does not exist when MongoDB does not do any action settings <upsert> will insert new data for us
<multi> default to False MongoDB in the update default only update lookup to a data set to true when the support multiline update setting multi to True must be together with the operation of the (otherwise error) because when you do not operate on multiple lines, You do not have to use the multi parameter
The above example shows the addition of fields for both documents in collection
Additional Update operations
$inc increase the value of an already existing key when the key does not exist to create a support number only
$rename renaming fields
This key is created $set the value used to specify a key does not exist.
$unset Delete the specified key value
MongoDB Basic Instruction Learning