Mongodb Guide (translation) (iii)-developer zone-Quick Start of MongoDB

Source: Internet
Author: User
Tags brew install mongodb install mongodb mongodb server mongo shell

This chapter is available in the official documentation at home-Quickstart.

 

Quick Start on OS X

Install MongoDB

The simplest way to install MongoDB is to use the Package Manager or pre-compiled binary files:

Package Manager

If you use homebrew Package Manager, run:

View plain
  1. $ Brew update
  2. $ Brew install MongoDB

If you use macports, you can install it as follows:

View plain
  1. $ Sudo port install MongoDB

The entire installation process takes a little time.

 

32-bit binary file

Note: We recommend that you use 64-bit (if you have a 64-bit operating system)

View plain
  1. $ Curl http://downloads.mongodb.org/osx/mongodb-osx-i386-x.y.z.tgz> Mongo. tgz
  2. $ Tar xzf Mongo. tgz

Replace x. y. Z with the version number of the current stable version.

 

64-bit binary file

View plain
  1. $ Curl http://downloads.mongodb.org/osx/mongodb-osx-x86_64-x.y.z.tgz> mongo. tgz
  2. $ Tar xzf mongo. tgz

Replace x. y. Z with the version number of the current stable version.

 

Create a data directory

By default, MongoDB stores data in/data/DB, but it does not automatically create this directory. Create as follows:

View plain
  1. $ Mkdir-p/data/db

You can also use the -- dbpath parameter to tell mongodb to use another data directory.

 

Run and connect to the server

First, start the mongodb server in a terminal:

View plain
  1. $./Mongodb-xxxxxxx/bin/mongod

Start shell on another terminal and it will automatically connect to the local machine by default:

View plain
  1. $./Mongodb-xxxxxxx/bin/mongo
  2. > Db. foo. save ({a: 1 })
  3. > Db. foo. find ()

Congratulations, you just used mongodb to store and retrieve your first document.

 

Quick Start in Linux

Download

Use the Package Manager

For Ubuntu and Debian users, see "Ubuntu and Debian packages ".

For CentOS and Fedora users, see "CentOS and Fedora Packages ".

 

32-bit linux binary file

Note: 64-bit is recommended.

View plain
  1. $ # Replace "1.6.4" in the url below with the version you want
  2. $ Curl http://downloads.mongodb.org/linux/mongodb-linux-i686-1.6.4.tgz> mongo. tgz
  3. $ Tar xzf mongo. tgz

64-bit Linux binary file

View plain
  1. $ # Replace "1.6.4" in the URL below with the version you want
  2. $ Curl http://downloads.mongodb.org/linux/mongodb-linux-x86_64-1.6.4.tgz> Mongo. tgz
  3. $ Tar xzf Mongo. tgz

Other Unixes

See "Download" and "generate ".


Create a data directory

By default, mongodb stores data in/data/db, but it does not automatically create this directory. In this way, create:

View plain
  1. $ Sudo mkdir-P/data/DB/
  2. $ Sudo chown 'id-U'/data/DB

You can also use the -- dbpath parameter to tell mongodb to use another data directory.

 

Run and connect to the server

First, start the mongodb server in a terminal:

View plain
  1. $./MongoDB-xxxxxxx/bin/mongod

Start shell on another terminal and it will automatically connect to the local machine by default:

View plain
  1. $./MongoDB-xxxxxxx/bin/Mongo
  2. > DB. Foo. Save ({A: 1 })
  3. > DB. Foo. Find ()

Congratulations, you just used mongodb to store and retrieve your first document.

 

Quick Start in Windows

Download

The simplest (recommended) method to install mongodb is to use the generated binary file. Note: We recommend that you have 64-bit windows.

32-bit binary file

Download and decompress the 32-bit compressed package. We recommend the "production" version.

64-bit binary file

Download and decompress the 64-bit compressed package.

Extract

Decompress the downloaded package to the local directory. For ease, you can also rename mongo-xxxxxx to mongo.

 

Create a data directory

By default, MongoDB stores data to \ data \ dB, but it does not automatically create this folder. You can create it as follows:

View plain
  1. C: \> mkdir \ data
  2. C: \> mkdir \ data \ db

 

Of course, you can also create this directory in the System Browser.

You can also use the -- dbpath parameter to tell MongoDB to use another data directory.

 

Run and connect to the server

Important binary files are:

  • Mongod.exe-database server. You can use mongod -- help to get more detailed startup help.
  • Cmd.exe-manage shell.

In the browser, double-click "cmd.exe" or start it in the command line:

View plain
  1. C: \> cd \ my_1__dir \ bin
  2. C: \ my_1__dir \ bin> mongod

Note: The server can also be started as a service.

Now, you can double-click cmd.exe or use the command line to start the Shell management. In bad condition, cmd.exe connects to the local server and uses a database named "test. Run Mongo -- help to obtain more information.

View plain
  1. C: \> cd \ my_1__dir \ bin
  2. C: \ my_1__dir \ bin> mongo
  3. > // The mongo shell is a javascript shell connected to the db
  4. > // By default it connects to database 'test' at localhost
  5. > 3 + 3
  6. 6
  7. > Db
  8. Test
  9. > // The first write will create the db:
  10. > Db. foo. insert ({a: 1 })
  11. > Db. foo. find ()
  12. {_ Id:..., a: 1}
  13. > Show dbs
  14. ...
  15. > Show collections
  16. ...
  17. > Help

Congratulations, you just stored and retrieved your first document through mongod.

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.