nodejs中使用monk访问mongodb

标签:业务范围    日期:2019-01-02 13:24    录入:美娱国际平台注册    浏览:

  

[node,mongodb]nodejs中使用monk访问mongodb

  

安装mongodb  

  

我觉得还是用mannual install靠谱一点儿:  

  

启动mongodb  

  
  
  $ mongod  
  
  

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

在nodejs中用monk访问mongodb  

  
  
  $ mkdir monk-app  $ cd monk-ap  
  
  

创建package.json  

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

创建app.js  

  

链接前面创建的数据库  

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

读取数据:  

  
  
  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' } ]  
  
  

插入数据:  

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

查找数据:  

  
  
  products.find({"name":"apple juice"}, 美娱国际平台 function(err, docs) {  console.log(docs)  })  
  
  

  

★★★小编:美娱国际 整理文章,欢迎大家转载 ★★★
上一篇:PS修图小教程!漂亮色调一秒调出来
下一篇:没有了
相关文章阅读