Use monk in nodejs to access mongodb

Source: Internet
Author: User
Tags install mongodb

Mongodb

Install mongodb

I think mannual install is a little more reliable: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/

Start mongodb

$ Mongod

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


Access 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 "})


Search data:

Products. find ({"name": "apple juice"}, function (err, docs ){
Console. log (docs)
})


Create an auto-increment id in mongodb:

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


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.