MongoDB Quick Start Detailed tutorials in Nodejs _node.js

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

MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for WEB applications.

MongoDB is a product between relational database and non relational database, and is the most powerful and relational database in the relational database.

First, the MongoDB must understand the concept

1. Database: Each database has its own permissions and collections.

2. Document: A key-value pair.

3. Collection: A set of documents, that is, a set of key-value pairs. When the first document is inserted, the collection is created.

Ii. MongoDB installation and start-up under Mac

1. Install using brew: Brew Install MongoDB.

2. Set up the catalogue: Mkdir-p/data/db. This establishes the default directory in the MongoDB configuration.

3. Set/data/db permissions to be readable and writable: Chown ' id-u '/data/db.

4. Run the Mongod command to open the server.

5. Create a new command terminal, enter MONGO, enter the MONGO command line tool (Mongod command cannot be paused) so you can enter various MONGO commands in the MONGO command-line tool.

Basic operation of MongoDB command line

1.show DBS: View Database

2.show Collections: View the collection of the current database

3.db. Collection_name.find (): View all documents under this collection

4.use database_name: Using a Database

5.db. Collection_name.insert ({"Key": "Value", "": "" ...}) : Inserts a document into a collection (note format)

6.db. Collection_name.find ({"A": 1}): Find a document with a value of 1

7.db.dropdatabase (): Deletes the database that is currently being use

Iv. use of MongoDB in node

1. Installation

Executing commands under the project directory: NPM install Mongodb-save-dev

It should be noted that the above requirements must be installed MongoDB to normal use of Nodejs in the MongoDB.

2. Basic use

(1) Connecting to the database:

<span style= "font-family:arial, Helvetica, Sans-serif;" >var mongoclient = require (' MongoDB '). Mongoclient; Introduce mongodb</span> 
[JavaScript] view plain copy print? View the code on the codes derived from my Code slice
var url = ' mongodb:// Localhost:27017/testproject '//Set the URL of the connection, note that the first half of the MongoDB database port, TestProject is the database to create, you can name 
Mongoclient.connect ( URL, function (err, db) { 
//... Note that DB is specified as the current database 
})

(2) Create Collection db.createcollection ()

var mongoclient = require (' MongoDB '). mongoclient; 
var url = ' Mongodb://localhost:27017/myproject '; 
Mongoclient.connect (URL, function (err, db) { 
console.log ("Connected successfully to Server"); 
Db.createcollection ("mycollection", {"capped": true, "size": 100000, "Max": 5000}, Function (err, results) { 
Console.log ("Collection Creation succeeded")}) 
;

(3) Inserting a document into the collection Collection.insertmany ()

var collection = Db.collection ("mycollection"); Select a collection 
collection.insertmany ([{a:1},{b:2},{c:3}],function (err, result) {//Insert data, where three data is inserted 
Console.log ( "Insert succeeded"); 
}

In this case, every time we execute our node code, we can view the results in the database and verify our actions. For example, here you can use Db.myCollection.find () to see if we have successfully written records.

(4) Find documents

Find All Documents:

var collection = Db.collection ("mycollection"); 
<span style= "White-space:pre" > </span>collection.find ({}). ToArray (function (err, docs) { 
<span style= "White-space:pre" > </span>console.log (Docs); Where the docs is the result of the obtained 
});

Find the content of the document that meets the criteria:

Collection.find ({"A": 1}). ToArray (function (err, docs) { 
<span style= "White-space:pre" > </span> Console.log (Docs); Returns the contents of the composite Condition 
});

The above is a small set to introduce the Nodejs in the MongoDB Quick Start detailed tutorials, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.