Use monk Access Mongodb_node.js in Nodejs

Source: Internet
Author: User
Tags install mongodb mongodb

Install MongoDB

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

Start MongoDB

$ mongod

Connect 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 "
}

Accessing MongoDB with Monk in Nodejs

$ 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 to 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)
})

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.