Nodejs using Monk to ask MongoDB

Source: Internet
Author: User

Mongodb

Installing MongoDB

I think it's a little bit more mannual install: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/

Start MongoDB

$ mongod

Connection Mogodb

$ MONGO

Mongo> Use Monk-app

Mongo> Db.products.insert ({"Name": "Apple Juice", "description": "Good"})

Writeresult ({"ninserted": 1})

Mongo> Db.products.find (). Pretty ()

{
"_id": ObjectId ("53b4cf8d5ef358e649ff1665"),
"Name": "Apple Juce",
"description": "Good"
}


Visit MongoDB in Nodejs with Monk

$ mkdir Monk-app

$ CD Monk-ap

Create Package.json

{
"Name": "Monk-app",
"Version": "0.0.1",
"Private": true,
"Dependencies": {
"MongoDB": "*",
"Monk": "*"
}
}

$ NPM Install


Create App.js

Link the database created earlier

var monk = require (' Monk ')
var db = Monk (' Localhost:27017/monk-demo ')

Read data:

var monk = require (' Monk ')
var db = Monk (' Localhost:27017/monk-demo ')

var products = Db.get (' products ')
Products.find ({}, function (err, docs) {
Console.log (Docs)
})

[{_id:53b4d3238cb4707ca35ab6f8,
Name: ' Apple Juice ',
Description: ' Good '}]


Insert data:

Products.insert ({"Name": "Orange Juice", "description": "Just So"})


Find data:

Products.find ({"Name": "Apple Juice"}, Function (err, docs) {
Console.log (Docs)
})


Create a self-increment ID in MongoDB:

http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/


Nodejs using Monk to ask MongoDB

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.