MOGODB Installation and use (Windows chapter)

Source: Internet
Author: User
Tags install mongodb

First, to install MongoDB

1.:http://www.mongodb.org/downloads or self- http://pan.baidu.com/s/1c1s3Heg

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 the 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 succeeds, and the default MongoDB listener port is 27017,mysql 3306

5. Test the connection

Open a new CMD window, enter the MongoDB bin directory, enter MONGO or Mongo.exe, the following message shows that the test passed, at this time we have entered the test database, how to enter the other database below will say.

 

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

> D:\mongodb\bin>mongod--dbpath "d:\mongodb\data\db"--logpath "D:\mongodb\data\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. You can also open the CMD input services.msc view service to see the MongoDB service

when the MongoDB service is started, it is sometimes reported that Windows cannot start, and the solution is:

1. MongoDB installation directory \data\ remove Mongod.lock from this folder

7. Shutting down services and deleting processes

> D:\mongodb\bin>net Stop MongoDB (Close 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. Common commands

    • Show DBS Display Database list
    • Use dbname into the dbname database, case sensitive, no this database does not matter
    • Show Collections displays a collection in the database, equivalent to a table

2. Create & Add

    • Db.users.save ({"Name": "LECAF"}) created a collection named users and added a new {"Name": "LECAF"} data
    • Db.users.insert ({"Name": "Ghost", "Age": ten}) inserts a new data into the Users collection, and if there is no users this collection, MongoDB automatically creates
    • There is a slight difference between save () and insert (): If the new data primary key already exists, insert () will not do the operation and prompt the error, while save () changes the original content to new content.
      • Existing data: {_id:1, ' name ': ' N1 '}, _id is the primary key
      • Insert ({_id:1, "name": "N2"}) will prompt for an error
      • Save ({_id:1, "name": "N2"}) will change the N1 to N2, with the function of update.

3. Delete

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

4. Find

    • Db.users.find () Find all data in the Users collection
    • Db.users.findOne () to find the first data in the Users collection

5. Modifications

    • Db.users.update ({"Name": "LECAF"}, {"Age": ten}) 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 will be replaced by the contents of the second parameter, the primary key cannot be modified,

 

Third, advanced applications

1. Condition Lookup

  • Db.collection.find ({"Key": value}) find data for Key=value
  • Db.collection.find ({"key": {$gt: Value}}) key > value
  • Db.collection.find ({"key": {$lt: Value}}) key < value
  • Db.collection.find ({"key": {$gte: Value}}) key >= value
  • 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.collection.find ({"key": {$mod: [10, 1]}}) modulo operation, equal to key% 10 = = 1 i.e. key divided by 10 + 1
  • Db.collection.find ({"key": {$nin: [1, 2, 3]}) does not belong, the condition equals the value of key does not belong to either [1, 2, 3]
  • Db.collection.find ({"key": {$in: [1, 2, 3]}), the condition equals key equal to either [1, 2, 3]
  • Db.collection.find ({"key": {$size: 1}}) $size number, size, condition equal to the number of values of key is 1 (key must be an array, a value can not be counted as an array of 1)
  • Db.collection.find ({"key": {$exists: True|false}}) $exists field exists, true returns data with field key, FALSE returns data with no Word key
  • Db.collection.find ({"Key":/^val.*val$/i}) regular, like; "I" Ignores case, "M" supports multiple lines
  • Db.collection.find ({$or: [{a:1}, {b:2}]}) $or or (note: MongoDB 1.5.3 later versions are available), eligible a=1 or eligible 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 criteria
  • Db.collection.find ({"Key.subkey": Value}) the values in the inline object match, note: "Key.subkey" must be quoted
  • Db.collection.find ({"key": {$not:/^val.*val$/i}}) This is an operator that is used in combination with other query conditions and is not used alone. The result set obtained by the above query condition plus $not can get the opposite set.

2. Sorting

    • Db.collection.find (). Sort ({"Key1":-1, "Key2": 1}) Here the 1 stands for ascending,-1 for 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 if it is not constrained
    • Db.collection.find (). Skip (5) controls how much the returned result skips, and 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 fetching 5 data
    • Db.collection.find (). Count (True) count () returns the number of result set bars
    • Db.collection.find (). Skip (5). Limit (5). Count (True) to obtain the actual number of results returned when adding skip () and limit (), a parameter of true is required. Otherwise, the total number of results that meet the query criteria is returned to: http://www.cnblogs.com/lecaf/

MOGODB Installation and use (Windows chapter)

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.