nodejs+mongoose连
来源:互联网 发布:caffe实现fcn 编辑:程序博客网 时间:2024/05/17 22:09
var mongoose = require('mongoose'),DB_URL = 'mongodb://192.168.5.128:27017/mongoosesample';/** * 建立连接 */function mongoConnection() { return mongoose.connect(DB_URL).connection;}/** * 判断连接状态 */mongoConnection() .on('error',function(err){ console.log('Mongoose connection error: ' + err); }) .on('disconnected',function () { console.log('Mongoose connection disconnected'); }) .on('connected',function () { console.log('Mongoose connection open to ' + DB_URL); });
如果需要改变mongodb的绑定URL,则修改mongodb配置文件 /etc/mongodb.conf
加上:bind_ip:192.168.5.128
连接的DB_URL,如果是本地就改为:
'mongodb://localhost:27017/mongoosesample';
------------------------------------------------------------------
连接到Mongodb之后,插入数据:
把下面的代码保存为:mongoTest.js
var mongoose = require('mongoose'),DB_URL = 'mongodb://192.168.5.128:27017/mongoosesample';module.exports= { mongoConn:mongoConnection(), schemaModule:schema_module()};/** * 建立连接 */function mongoConnection() { return mongoose.connect(DB_URL).connection;}/** * 判断连接状态 */mongoConnection() .on('error',function(err){ console.log('Mongoose connection error: ' + err); }) .on('disconnected',function () { console.log('Mongoose connection disconnected'); }) .on('connected',function () { console.log('Mongoose connection open to ' + DB_URL); });/** * 创建module */function schema_module() { var Schema = mongoose.Schema; //声明Schema var nodeSchema = new Schema({ name: String, email: String }); //构建model return mongoose.model('Info', nodeSchema);}
然后新建一个test.js
let mongoTest = require('./mongoTest')mongoTest.mongoConnmongoTest.schemaModule.create({'name':"zhang","email":"zhang@163.com"},function(err){ if(err){ console.log(err); }else{ console.log('The new node is saved'); }});
执行test.js就可以把
{'name':"zhang","email":"zhang@163.com"}
插入到infos这个文档中。
------------------------------------------------------------------------------
查询数据:
在test.js中加入查询数据的代码
let mongoTest = require('./mongoTest')mongoTest.mongoConnmongoTest.schemaModule1.create({'name':"liu","email":"mmm"},function(err){ if(err){ console.log(err); }else{ console.log('The new node to info is saved'); }});mongoTest.schemaModule2.create({'t1':"t11","t2":"t22","t3":"t33"},function(err){ if(err){ console.log(err); }else{ console.log('The new node to table is saved'); }});let query = mongoTest.schemaModule1.find({});query.find({name: "liu"});//或:query.where({name: "liu"})query.exec(function (err, data) { if (err) console.log(err); else{ console.log(data)}});
let query = mongoTest.schemaModule1.find({});不加查询条件,则返回的是整 个文档。
-----------------------------------------------------
删除
mongoTest.schemaModule1.remove({});
阅读全文
0 0
- nodejs+mongoose连
- nodejs+mongoose()连表查询
- NodeJS 安装mongoose(备忘)
- nodejs/express+mongodb/mongoose
- nodejs mongoose示例
- Nodejs mongoose操作mongodb
- nodejs mongoose 数据库处理
- [nodejs]mongoose学习1
- 7.Nodejs入门-----Mongoose
- 快速上手Nodejs mongoose
- nodejs+express+ejs+mongoose实例
- nodejs+express+ejs+mongoose实例
- nodejs+express+ejs+mongoose实例
- NODEJS mongoose 增删改查
- [NodeJS] Mongoose Populate 基本使用
- nodejs使用mongodb和mongoose
- NodeJS实战:Express+Mongoose+ejs
- Mongoose简单的连表查询
- Android根据byte数组,生成图片,图片名字以当前时间作为名字
- Nginx配置SSI
- OutOfMemoryError异常总结
- 2017 Multi-University Training Contest
- php接口接收json数据
- nodejs+mongoose连
- 单例模式
- 关于radio 单选框在项目中的一些心得
- leetcode10
- 超绚丽的HTML5地图分布动画
- 一个简易的“返回顶部”页面效果
- linux top命令参数详细介绍
- 网络基础知识(一)半双工与全双工
- 613. Shortest Distance in a Line--ABS() and MIN()