First install MongoDB
1.:http://www.mongodb.org/downloads
2. Unzip to the directory you want to install, such as F:\mongodb
3. Create folders F:\mongodb\db, F:\mongodb\log, respectively, to install the DB and log files, create a log file under the log folder Mongodb.log, that is, D:\mongodb\log\mongodb.log
4. Run cmd into the DOS command interface and execute the following command
> CD F:\mongodb\bin
> F:\mongodb\bin>mongod-dbpath "F:\mongodb\db"
The default MongoDB listener port is 27017
5. Open a new CMD window, enter the MongoDB bin directory, enter MONGO or Mongo.exe, if the test passes, then we have entered the test database. Enter exit or CTRL + C to exit.
6. When the Mongod.exe is turned off, Mongo.exe will not be able to connect to the database, so each time you want to use the MongoDB database to open the Mongod.exe program, it is more troublesome, at this time we can install MongoDB as a Windows service
Or run cmd, go to the Bin folder and execute the following command:
> F:\mongodb\bin>mongod--dbpath "F:\mongodb\db"--logpath "F:\mongodb\log\MongoDB.log"--install--servicename "MongoDB"
Here MongoDB.log is the log file that was started,--servicename the "MongoDB" service named MongoDB
Then start the MongoDB service
> D:\mongodb\bin>net START MongoDB
Open Task Manager and you can see that the process has started
7. Shutting down services and deleting processes
> F:\mongodb\bin>net Stop MongoDB (Close service)
> F:\mongodb\bin>mongod--dbpath "F:\mongodb\db"--logpath "F:\mongodb\log\MongoDB.log"--remove--servicename " MongoDB "(Delete, note not--install)
8. If you do not want to switch directories each time you run, you can add the installation path of MongoDB to path, as in the case: F:\mongodb\bin
--Create library use navy --Display library show db; Show dbs --Create Collection db.createcollection ("mycollection", {"Autoindexid": true}); --can be db.help () query the specific usage way --Display collection show collections --Delete Collection Db.collection_name.drop (); -- Add Data Db.mycollection.insert ([{"Name": "YC", "addr": "Hunan Yiyang"},{"name": "Navy", "addr": "Hunan Hengyang"}]) Db.mycollection.insert ( {"_id": 1, "Company": "YC", "url": "Www.hyycinfo.com", "pageviews": 1}) Db.mycollection.insert ({"_id": 2, "Company": "Ycs", "url": "Www.hyycinfos.com", "pageviews": 1}) --save () method Replace if present in the collection, otherwise add Db.mycollection.save ({"_id": 1, "Company": "Ycinfo", "url": "Www.hyycinfo.com", "pageviews": 1}) --Replace Db.mycollection.save ({"_id": 3, "Company": "YC", "url": "Www.hyycinfo.com", "pageviews": 1}) --add --the basic syntax for modifying the update () method is as follows: DB. Collection_name.update (Selectioin_criteria, updated_data) --$inc modifier adds pageviews value 1db.mycollection.update ({ "_id": 1},{"$inc": {"pageviews": 1}}) --$setdb. Mycollection.update ({"_id": 2},{"$set": {"pageviews": 100, " Company ":" Navy "})--Changes the corporation to an array db.mycollection.Update ({"_id": 2},{"$set": {"Company": ["Navy", "YC", "HG"]}}) --Delete Company Health db.mycollection.update ({"_id": 2},{" $unset ": {" Company ": 1}}) --array modifier--$push add element db.mycollection.update ({" _id ": 2},{" $push ": {" Company ":" SC "}}) --$each Add multiple db.mycollection.update ({"_id": 2},{"$push": {"Company": {"$each": ["RC", "ZR"]}}) --$slice To guarantee the length of the array db.mycollection.update ({"_id": 2},{"$push": {"Company": {"$each": ["RC", "ZR", "Navy", "YC", "HG"], "$ Slice ": -10}}" --retains only the last 10 digits, $slice must be negative --$ne/$addToSet when adding an element to the array type key value, avoid generating duplicate data in the array, $ne in some cases is not in transit. Db.mycollection.update ({"_id": 2},{$addToSet: {"Company": "YC"}}) --unsuccessful db.mycollection.update ({"_id": 2},{$ addtoset:{"Company": "Yc1"}) --successful --$pop Delete element {$pop: {"<key>": 1}} removes an element from the end of the array. {$pop: {"<key>":-1}} Remove an element from the head db.mycollection.update ({"_id": 2},{$pop: {"Company": 1}}) --$ Pull removes all matching elements from the array db.mycollection.update ({"_id": 2},{$pull: {"Company": "ZR"}}) --$ position-based array modifier if the array has multiple values, And we just want to make changes to some of them, either by location or by locating the operator "$" array the following table is starting from 0, and the subscript can be straightTo select the element as a key. For example: Db.blog.insert ({"_id": 1001, "Content": "Source Chen Information Technology Co., Ltd.", "comments": [{"Comment": "Good Company", "author": "Army", "votes": 100} , {"Comment": "Learning Paradise", "Author": "Small Army", "votes": 1001},{"comment": "The Land of Happy Growth", "Author": "Little Army", "votes": 100}]}) If you want to increase the number of votes for the first comment, you can do so; Db.blog.update ({"_id": 1001},{$inc: {"comments.0.votes": 1}) But in many cases, You cannot know the small label of the array you want to modify without querying the document in advance. To overcome this difficulty, MongoDB provides the positioning operator $, which is used to locate the array elements that the query document has matched and to update. Db.blog.update ({"Comments.author": "Small Army"},{$set: {"Comments.$.author": "Xiao June elder brother"}}); mongodb will only update a single file by default. Update multiple you need to set the parameter ' Multi ' to Truedb.blog.update ({' title ': ' MongoDB overview '},{$set: {' title ': ' New MongoDB Tutorial '}},{ Multi:true}) db.blog.update ({' title ': ' MongoDB overview '},{$set: {' title ': ' New MongoDB Tutorial '}},false,true '); --condition Query Db.mycollection.find () Db.mycollection.findOne (). pretty (); --pretty () formatted output db.mycollection.find ({"<key>": "<val>"})--where <key> = <val> Db.mycollection.find ({"<key>": {$lt: <val>}})--where <key> < <val>db.mycollection.find ({ "<keY> ": {$lte: <val>}})--where <key> <= <val>db.mycollection.find ({" <key> ": {$gt: <val })--where <key> > <val>db.mycollection.find ({"<key>": {$gte: <val>}})--where <key > >= <val>db.mycollection.find ({"<key>": {$ne: <val>}})--where <key>! = <val> --indb.mycollection.find ({"<key>": {$in: [<val>]}}) --anddb.mycollection.find ({"Name": " Navy "," addr ":" Hunan Hengyang "}). Pretty ()--where <key>! = <val> --ordb.mycollection.find ({$or: [{" Name ":" Navy "},{" addr ":" Hunan Hengyang "}"}). Pretty () --and and Ordb.mycollection.find ({"Name": "YC", $or: [{"Name": "Navy"} {"Addr": "Hunan Hengyang"}]}). Pretty () cursor operation for (i=0;i<100;i++) {Db.collection.insert ({x:i});} var cursor=db.collection.find (); var obj;while (Cursor.hasnext ()) {Obj=cursor.next ();d isplay (obj);} Cursors also implement the iterative interface of JavaScript, so you can use Cursor.foreach (function (x) {print (x)}) in a ForEach loop; db.collection.find (). Limit ( 3)--return to the top three Db.collectiOn.find (). Skip (3); --Skip the top three sort to accept an object as a parameter, which is a set of key/value pairs, the key corresponding to the document's key name, and the value is the collation. 1 is ascending,-1 is descending. Db.collection.sort ({username:1,age:-1}); page 10 of each page the first page: Db.collection.find ({"desc": "MP3"}). Limit ({"Price":-1}); Next: Db.collection.find ({"desc": "MP3"}). Skip (Ten). Limit ({"Price": -1}); Index: Db.collection.ensureIndex ({"Age": 1, "username": 1})--Establish compliant indexes on age and username
Some basic operations of MongoDB