Home  >  Article  >  Web Front-end  >  Use monkey to access mongodb_node.js in nodejs

Use monkey to access mongodb_node.js in nodejs

WBOY
WBOYOriginal
2016-05-16 16:42:321690browse

Install mongodb

I think it is more reliable to use 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"
}

Use monk to access mongodb 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 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 so"})

Find data:

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn