NoSQL database changes the disadvantage of relational database, easier integration, distributed, no mode, failure recovery and other characteristics, is a step by step meal relationship Database market, as a time-keeping with the code of the farmers to understand the new technology is necessary, especially after reading the "NoSQL pristine", You want to experience the power of a NoSQL database.
MongoDB is a document database, which means that for each aggregation in the domain model, it is stored as a document.
MongoDB has the following advantages:
Document-oriented storage, full-index support, synchronization mechanisms and high accessibility, automatic sharding, querying, flexible aggregation and data processing, mapping simplification, file storage.
First step: Install
Since version 2.2, MongoDB does not support the XP operating system, the egg hurts, now I am using XP, it seems only to use the version before 2.2,
Available version only a 2.0.9, I have shared to Baidu network disk,: HTTP://PAN.BAIDU.COM/S/1I3GES1V, if it fails, please go to the official website to download.
Step two: Set up the database environment and start the database
Unzip the compressed version and put it on any hard drive, like I put it in the E:\mongodb-win32-i386-2.0.9.
Create a folder to hold the document file, E:\mongodb-win32-i386-2.0.9\data This folder is used to hold the data document
Once created, configure this folder to MongoDB and let MongoDB store the data in this folder.
To configure and start the database:
Run command line:E:\mongodb-win32-i386-2.0.9\bin\mongod.exe--dbpath E:\mongodb-win32-i386-2.0.9\data
(if not specified, the data document is stored by default in the path C:\data when MongoDB starts)
Step Three:
Once the database is started, we can connect to the database to access and store the data.
Another command window and run: E:\mongodb-win32-i386-2.0.9\bin\mongo.exe--dbpath E:\mongodb-win32-i386-2.0.9\data
The following parameter is the data directory to which you want to connect
By default MongoDB will select a database named Test, and if you do not know the current database, you can run the DB command to view
To view all databases:
Show DBS
To switch databases:
Use MyDB
If the database you are switching to does not exist, MongoDB does not create the database on the hard disk as long as it does not hold data to the database.
It's time to store the data, let's talk about some of the concepts in MongoDB.
In MongoDB, DB, collection, document,db correspond to the database in the relational database, while collection corresponds to the table in the relational database, and document corresponds to a row of data in the relational database table. The document in MongoDB is like a JSON file, and we can create an object literal in a JavaScript as a document, as defined by two object variables as follows:
j = {name: "MONGO"= {X:3}
Save the two documents, J and K, in a collection named "TestData"
Db.testData.insert (j) Db.testData.insert (k)
As already mentioned, DB is the current database object, and TestData is a collection, and there is no corresponding collection at this time, and when insert is finished, the collection object is created
We can pass
Show collections
command to view all collection in the current database
The testdata and system.indexes are returned, and System.indexes is MongoDB's own collection, without it
We can query all the data in TestData by the following statement
Db.testData.find ()
The Find method now returns a Cursor object
Because there is no variable to receive the cursor object at this time, it will default to print up to 20 data (only the two data just now, as we only have these two in our testdata)
{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "Mongo""_id": ObjectId ("4c2209fef3924d31102bd84b"), "X": 3}
The _id property is automatically generated by MongoDB because each document has a "primary key" (much like the primary key in the relational database) and we do not provide _id at this time.
Let's try to add more data:
On the command line, enter:
for (var i = 1; i <=; i++) Db.testData.insert ({x:i})
A For loop that loops through 25 of data
We'll do it again.
Db.testData.find ()
Result output
{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "MONGO" }{ "_id": ObjectId ("4c2209fef3924d31102bd84b"), "X": 3 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990be6"), "X": 1 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990be7"), "X": 2 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990be8"), "X": 3 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990be9"), "X": 4 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bea"), "X": 5 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990beb"), "X": 6 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bec"), "X": 7 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bed"), "X": 8 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bee"), "X": 9 }{ "_id": ObjectId ("51A7DC7B2CACF40B79990BEF"), "X": 10 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bf0"), "X": 11 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bf1"), "X": 12 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bf2"), "X": 13 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bf3"), "X": 14 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bf4"), "X": 15 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bf5"), "X": 16 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bf6"), "X": 17 }{ "_id": ObjectId ("51a7dc7b2cacf40b79990bf7"), "X": 18}has More
Only 20 data was output
There is also a more hint behind the data
Then enter it on the command line to output the next batch of 20 data
How can I output all the data?
At this point we use a variable to receive the cursor:
var c = Db.testData.find ()
C is the cursor object
At this point we output all the data through a For loop:
while (C.hasnext ()) Printjson (C.next ())
The Printjson method outputs the document in JSON format
The Hasnext method of the cursor object is used to determine if there is a next document, and the next method is used to get the next document.
What if we just want to get a 5th document?
Of course you can count the traversed document while traversing the cursor and output the 5th document, but this is a bit verbose.
We can now add a subscript directly on the cursor object, such as:
Printjson (c [4])
The 5th document is now output
{"_id": ObjectId ("51a7dc7b2cacf40b79990bea"), "X": 5}
However, please note at this point:
The C [4] method reads all the documents under the collection into memory, which is equivalent to executing the Cursor.toarray () method on the cursor,
This method loads all of the documents into memory, and then looks for a document in the returned array that has an index value of 4, so the cursor subscript method should be used with caution.
What should we do if we want to query the document A is 18?
We can also use the Find method at this point, but to provide a template document for the Find method
A template document detailing the query strategy, such as: Db.testData.find ({x:18})
At this point {x:18} is a template document, which means that only documents with X 18 are retained when querying documents.
The query results are as follows:
{"_id": ObjectId ("51a7dc7b2cacf40b79990bf7"), "X": 18}
Don't forget, the Find method returns a cursor object, except that only one document in the query matches the criteria at this time.
If you want to return only one document instead of a cursor object, you can use the FindOne method, which returns a Document object, regardless of how many documents match the criteria, and returns only the first one.
If you want to limit the number of documents returned, you can call the Limit method on the cursor, as follows:
Db.testData.find (). Limit (3)
Only the first 3 documents are returned
The results are as follows:
{"_id": ObjectId ("51a7dc7b2cacf40b79990be6"), "X": 1"_id": ObjectId ("51a7dc7b2cacf40b79990be7"), "X": 2 "_id": ObjectId ("51a7dc7b2cacf40b79990be8"), "X": 3
Reference Link: http://docs.mongodb.org/manual/