Windows MongoDB installation and use of graphics and text tutorial (finishing) _mongodb

Source: Internet
Author: User
Tags install mongodb mongodb

First, install MongoDB

1. Download Address: Http://www.mongodb.org/downloads

2. Unzip to the directory you want to install, such as D:\mongodb

3. Create folders d:\mongodb\data\db, D:\mongodb\data\log, respectively to install DB and log files, create a log file under the log folder MongoDB.log, that is, d:\mongodb\data\log\ MongoDB.log

4. Run cmd.exe into the DOS command interface and execute the following command

> CD D:\mongodb\bin

> D:\mongodb\bin>mongod-dbpath "D:\mongodb\data\db"

If you see a similar message, the boot is successful and the default MongoDB listener port is 27017,mysql 3306

5. Test Connection

Open a new CMD window, into the MongoDB bin directory, input MONGO or mongo.exe, the following information appears to show the test passed, at this time we have entered test this database, how to enter the other database will be said below.

 

Enter exit or CTRL + C to exit.

6. When the Mongod.exe is closed, Mongo.exe can not connect to the database, so every time you want to use the MongoDB database to open the Mongod.exe program, so more trouble, at this time we can install MongoDB as Windows services

Or run cmd, go to the Bin folder, and execute the following command

> D:\mongodb\bin>mongod--dbpath "d:\mongodb\data\db"--logpath "D:\mongodb\data\log\MongoDB.log"--install-- ServiceName "MongoDB"

Here MongoDB.log is the start of the log file,--servicename "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. Shutdown service and delete process

> D:\mongodb\bin>netstop MongoDB (off service)

> D:\mongodb\bin>mongod--dbpath "d:\mongodb\data\db"--logpath "D:\mongodb\data\log\MongoDB.log"--remove-- ServiceName "MongoDB" (delete, note not--install)

Ii. Use of MongoDB

1. Commonly used commands

Show DBS Display Database list use dbname Enter dbname database, case sensitive, no this database does not matter show collections display a collection in a database, equivalent to a table

2. Create & Add

Db.users.save ({"Name": "LECAF"}) created a collection named users, and added a data db.users.insert {"name": "LECAF"}
 ({"Name": "Ghost", " Age ": 10}" Inserts a new data in the Users collection, and if there is no users this collection, there
 is a slight difference between the MongoDB creation of Save () and insert (): If the new data primary key already exists, insert () Does not do the operation and prompts for an error, and save () changes the original content to the new content.
 • Presence data: {_id:1, ' name ': ' N1 '}, _id is the primary key
 insert ({_id:1, "name": "N2"}) prompts for error
 Save ({_id:1, " Name ":" N2 "})  will change the N1 to N2, with the role of update.

3. Delete

Db.users.remove () deletes all data Db.users.remove under the Users collection
 ({"Name": "LECAF"}) to delete the data NAME=LECAF under the Users collection
 · Db.users.drop () or Db.runcommand ({"Drop", "users"}) deletes the collection users
 db.runcommand ({"Dropdatabase": 1}) deletes the current database

4. Find

Db.users.find () Find all data in the Users collection
 db.users.findone () find the first data in the Users collection

5. Revise

db.users.update ({"Name": "LECAF"}, {"Age": 10}) modifies the NAME=LECAF data to age=10, the first parameter is the lookup condition, the second parameter is the modified content, except the primary key, Other content is replaced by the contents of the second parameter, and the primary key cannot be modified

As shown in figure

 

Third, advanced application

1. Condition Lookup

Db.collection.find ({"Key": Value}) finds the Key=value data db.collection.find ({"key": {$gt: Value}}) key > Value D B.collection.find ({"key": {$lt: Value}}) key < value Db.collection.find ({"key": {$gte: Value}}) key >= V  Alue Db.collection.find ({"key": {$lte: Value}}) key <= value Db.collection.find ({"key": {$gt: value1, $lt: value2}}) value1 < key <value2 Db.collection.find ({"key": {$ne: Value}}) key <> value Db.collectio N.find ({"key": {$mod: [10, 1]}}) modulo operation, the condition equals key% 10 = 1 that is the key divided by 10 remainder is 1 db.collection.find ({"key": {$nin: [1 , 2, 3]}} does not belong, the condition equivalent to the value of key does not belong to [1, 2, 3] any one of the Db.collection.find ({"key": {$in: [1, 2, 3]}}) belongs, the condition equals key equals [1, 2 , 3] Any one of the Db.collection.find ({"key": {$size: 1}}) $size quantity, size, the number of values equal to key is 1 (key must be an array, the case of a value cannot be counted as an array of 1) Db.col  Lection.find ({"key": {$exists: True|false}}) $exists field exists, true returns data with field key, FALSE returns data Db.collection.find without Word degree key ({ "Key":/^val.*val$/i}) Regular, similar to like; "I" Ignores case, "M" Supports multiline Db.collection.find ({$or: [{a:1}, {b:2}]}) $or or (note: MongoDB 1.5.3 version available), eligible a=1 or compliant b=2 data will be queried Db.collection.find ({"Key": Value, $or: [{a:1}, {b:2}]}) meets the criteria Key=value and matches any of the other two conditions D B.collection.find ({"Key.subkey": Value}) the values in the inline object match, note: "Key.subkey" must be quoted Db.collection.find ({"key": {$not:/^val.*v al$/i}}) This is an operator that is used in combination with other query criteria and will not be used separately. The result set obtained by the above query condition plus the $not can get the opposite set.

2. Sorting

Db.collection.find (). Sort ({"Key1":-1, "Key2": 1}) The 1 here represents ascending, and-1 represents descending

3. Other

db.collection.find (). Limit (5) controls the number of returned results, and if the argument is 0, the limit () will not work
 db.collection.find (). Skip (5) Controls how much of the return results are skipped, if the argument is 0, then skip () will not work, or skip 0
 db.collection.find (). Skip (5). Limit (5) can be used for paging, skipping 5 data and then 5 data
 Db.collection.find (). Count (True) count () returns the number of bars of the result set
 db.collection.find (). Skip (5). Limit (5). Count (True) When you add both skip () and limit (), to get the actual number of results returned, you need a parameter true, otherwise the total number of results that match the query criteria is returned.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.