Home > Web Front-end > JS Tutorial > body text

Use monkey to access mongodb_node.js in nodejs

WBOY
Release: 2016-05-16 16:42:32
Original
1697 people have browsed it

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
Copy after login

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

Copy after login

Use monk to access mongodb in nodejs

$ mkdir monk-app

$ cd monk-ap

Copy after login

Create package.json

{
 "name": "monk-app",
 "version": "0.0.1",
 "private": true,
 "dependencies": {
  "mongodb": "*",
  "monk": "*"
 }
}

Copy after login
$ npm install
Copy after login

Create app.js

Link the database created earlier

var monk = require('monk')
var db = monk('localhost:27017/monk-demo')
Copy after login

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' } ]
Copy after login

Insert data:

products.insert({"name":"orange juice","description":"just so so"})
Copy after login

Find data:

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!