MongoDB Use Experience Summary

Source: Internet
Author: User
Tags brew install mongodb install mongodb

Summary:

Recently in the development project using the database MongoDB, I will use its methods to organize to share to everyone. As for MongoDB has any advantages, we can go to the official website to see.

Installation:

First we need to go to the official website to download the appropriate MONGODB for your system.

Windows:

You can enter the following command in the Command window to view your system parameters, select the corresponding version

Osarchitecture

If you are downloading a zip file, just follow the steps below to install it:

1. Unzip the downloaded file to the directory you want to install, for example: D:\test\. Change the extracted folder name to MongoDB and include the bin file in the MongoDB file.

2, create the data and log warehouse, new folder D:\test\mongodb\data\db and D:\test\mongodb\data\log, a new log file MongoDB.log under the log folder.

3, set the Data Warehouse path and start, mongodb default Data Warehouse is \DATA\DB, we need to change the path set for ourselves, in the Command prompt window input

>d:

D:>CD D:\test\mongodb\bin

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

If the console sees a similar message, it means the installation was successful.

Note: Close the window and close the MongoDB service, if you want to turn off the service you can use CTRL + C to close.

4, test the connection, reopen a command window, go to the above directory, and then enter MONGO or Mongo.exe when the following message appears to indicate that the test passed, at this time we have entered the MONGO default database test this database.

5, the above mentioned close window MONGO service is closed, so that every time we need MongoDB service to open Mongod.exe program, more trouble. We can set MONGO as the default service for Windows.

Execute the following command under the bin directory of MongoDB

Mongod--dbpath "D:\test\mongodb\data\db"--logpath "D:\test\mongodb\data\log\MongoDB.log"--install--servicename " MongoDB "

Detailed parameters:

–install: Installation

–remove : Remove

–servicename <name>: Service Name

–servicedisplayname <display-name>: Service List Display Name

–servicedescription <description>: Service description

Once set up, you only need to execute the following command to start MongoDB.

NET START MongoDB

6. Close and delete

Shut down:

Mongodb

Delete:

SC.  MongoDB 

If you are downloading an MSI file, simply double-click the installation file and install it step-by-step. Finally you will find the MongoDB 2.6 standard file in the C:\Program files directory. This file is similar to the zip file we downloaded, we only need to follow the zip file installation method.

Mac:

If your system is a Mac, there are two ways to install it

1, using brew, only need to execute the following command to install MongoDB

Brew Install MongoDB
Or
Brew Install MongoDB--with-openssl
In or
Brew Install MongoDB--devel

2. General Installation

Down the file pack first

Curl-o http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.6.6.tgz
Extract
TAR-ZXVF mongodb-osx-x86_64-2.6.6.tgz

Then copy the files to the installation directory

Mkdir-p mongodbcp-r-N mongodb-osx-x86_64-2.6.6/mongodb

3. Configure the installation path to RC file

PATH=<mongodb-install-directory>/bin:$PATH 

4. Create a Data Warehouse

Mkdir-p/data/db

5. Set the data path

Mongod--dbpath <path to Data directory>

Note : Do not make the Mongod.exe file visible within the public network because MongoDB is executed in a trusted environment.

Operational database:

 Start MongoDB, create a database mongotest

Use Mongotest;

Inserting data into the user table of Mongotest

Db.user.insert ({
Name: ' Zhangsan ',
Age:20
});

We operate the database in the node environment.

mkdir myprojectcd myproject

Create the Package.json file as follows

{  "name": "MyProject",  "version": "1.0.0",  "description": "My first Project",  "main": "Index.js",  "Repository": {    "type": "Git",    "url": "Git://github.com/christkv/myfirstproject.git"  },  "dependencies": {    "MongoDB": "~2.0"  },  "author": "Christian Kvalheim",  "license": "Apache 2.0 ",  " bugs ": {    " url ":" Https://github.com/christkv/myfirstproject/issues "  },  " homepage ":" Https://github.com/christkv/myfirstproject "}

Execute the NPM install command to install the MongoDB module. Create a new file Mongotest.js with the following content:

varMongoDB = require (' MongoDB ');varServer =NewMongodb. Server ("127.0.0.1", 27017,{});//Local 27017 PortNewMongodb. Db (' Mongotest ', server,{}). Open (function(error,client) {//Database: Mongotest    if(Error)Throwerror; varCollection =NewMongodb. Collection (client, ' user ');//table: UserCollection.find (function(error,cursor) {Cursor.each (function(error,doc) {if(DOC) {Console.log ("Name:" +doc.name+ "Age:" +doc.age);    }        }); });});

Run:

Node Mongodbtest.js

The output result is

Command:

1. Basic

Show DBS Show Database list
Use DB Enter DB database
Show collections Displaying collections in a database

2, increase

Db.table.save ({"id": 1}) Create a table named tables and add a new piece of data
Db.table.insert ({"id": 1}) Insert a new piece of data into the table, and if there is no table table, MongoDB automatically creates

3, change

Db.users.update ({"id": 10},{"name": "Zhangsan"}) Modify the id=10 record Name=zhangsan, the first parameter is the lookup condition, the second parameter is the modification, in addition to the primary key, the other content will be replaced by the contents of the second parameter, the primary key cannot be modified

4. By deleting

Db.table.remove () Delete all data for table tables, but the table still exists
Db.table.remove ({"id": 1}) Delete id=1 records for table tables
Db.table.drop () Delete Tables Table
Db.runcommand ({"Drop", "Table"}) Delete Tables Table
Db.runcommand ({"Dropdatabase": 1}) Delete current Database

5. Check

Db.table.find () Find all of the data under table tables

Db.collection.find ({"Key": Value})

Finding data for Key=value
Db.table.findOne () Find the first record in table tables
Db.collection.find ({"key": {$gt: Value}}) Find data for key > value

6. Sorting

Db.collection.find (). Sort ({"Key1":-1, "Key2": 1})

The results of the lookup are in descending order from the first field, and the second word orderby

Appendix:

http://docs.mongodb.org/manual/

Https://github.com/mongodb/node-mongodb-native

MongoDB Use Experience Summary

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.