[转] node.js下mongoose简单操作实例
Mongoose API : http://mongoosejs.com/docs/api.html
// mongoose 链接
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://127.0.0.1:27017/NodeJS');
// 链接错误
db.on('error', function(error) {
console.log(error);
});
// Schema 结构
var mongooseSchema = new mongoose.Schema({
username : {type : String, default : '匿名用户'},
title : {type : String},
content : {type : String},
time : {type : Date, default: Date.now},
age : {type : Number}
});
// 添加 mongoose 实例方法
mongooseSchema.methods.findbyusername = function(username, callback) {
return this.model('mongoose').find({username: username}, callback);
}
// 添加 mongoose 静态方法,静态方法在Model层就能使用
mongooseSchema.statics.findbytitle = function(title, callback) {
return this.model('mongoose').find({title: title}, callback);
}
// model
var mongooseModel = db.model('mongoose', mongooseSchema);
// 增加记录 基于 entity 操作
var doc = {username : 'emtity_demo_username', title : 'emtity_demo_title', content : 'emtity_demo_content'};
var mongooseEntity = new mongooseModel(doc);
mongooseEntity.save(function(error) {
if(error) {
console.log(error);
} else {
console.log('saved OK!');
}
// 关闭数据库链接
db.close();
});
// 增加记录 基于model操作
var doc = {username : 'model_demo_username', title : 'model_demo_title', content : 'model_demo_content'};
mongooseModel.create(doc, function(error){
if(error) {
console.log(error);
} else {
console.log('save ok');
}
// 关闭数据库链接
db.close();
});
// 修改记录
mongooseModel.update(conditions, update, options, callback);
var conditions = {username : 'model_demo_username'};
var update = {$set : {age : 27, title : 'model_demo_title_update'}};
var options = {upsert : true};
mongooseModel.update(conditions, update, options, function(error){
if(error) {
console.log(error);
} else {
console.log('update ok!');
}
//关闭数据库链接
db.close();
});
// 查询
// 基于实例方法的查询
var mongooseEntity = new mongooseModel({});
mongooseEntity.findbyusername('model_demo_username', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
// 基于静态方法的查询
mongooseModel.findbytitle('emtity_demo_title', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
// mongoose find
var criteria = {title : 'emtity_demo_title'}; // 查询条件
var fields = {title : 1, content : 1, time : 1}; // 待返回的字段
var options = {};
mongooseModel.find(criteria, fields, options, function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
// 删除记录
var conditions = {username: 'emtity_demo_username'};
mongooseModel.remove(conditions, function(error){
if(error) {
console.log(error);
} else {
console.log('delete ok!');
}
//关闭数据库链接
db.close();
});
[转] node.js下mongoose简单操作实例的更多相关文章
- node.js下mongoose简单操作实例
Mongoose API : http://mongoosejs.com/docs/api.html // mongoose 链接var mongoose = require('mongoose'); ...
- Node.js使用Mongoose包操作MongoDB数据库
1. 安装Mongoose npm install mongoose 2. 使用 2.1 创建连接 var mongoose = require('mongoose'); mongoose.conne ...
- node.js下操作cookie
cookie,又是cookie.工作中与cookie打交道很多次,不过时间跨度也大,每总结多一次,就加深了解多一点. cookie,一定是放在浏览器中的,用于浏览器保存一些小额度的内容.每次我们去访问 ...
- [js高手之路]Node.js+jade+mongoose实战todolist(分页,ajax编辑,删除)
该系列文章索引: [js高手之路]node js系列课程-创建简易web服务器与文件读写 [js高手之路]node js系列课程-图解express+supervisor+ejs用法 [js高手之路] ...
- node.js 下依赖Express 实现post 4种方式提交参数
上面这个图好有意思啊,哈哈, v8威武啊.... 在2014年的最后一天和大家分享关于node.js 如何提交4种格式的post数据. 上上一篇说到了关于http协议里定义的4种常见数据的post方法 ...
- npm 是node.js下带的一个包管理工具
npm 是node.js下带的一个包管理工具 npm install -g webpack webpack是一个打包工具 gulp是一个基于流的构建工具,相对其他构件工具来说,更简洁 ...
- VIM - visual selection 模式下的简单操作
1. 概述 vim 的 visual selection 模式下的简单操作 2. visual selection 模式 概述 可视化选择 可视化选择 vim 的一种专门用来选择的模式 可以提供相对于 ...
- Practical Node.js (2018版) 第7章:Boosting Node.js and Mongoose
参考:博客 https://www.cnblogs.com/chentianwei/p/10268346.html 参考: mongoose官网(https://mongoosejs.com/docs ...
- 手把手教你学node.js之一个简单的express应用
一个简单的express应用 目标 建立一个 lesson1 项目,在其中编写代码.当在浏览器中访问 http://localhost:3000/ 时,输出 Hello World. 挑战 访问 ht ...
随机推荐
- Python运维开发基础04-语法基础【转】
上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 仅用列表+循环实现“简单的购物车程 ...
- C# 操作文件类,文件夹存在判断,创建,文件内容读写。
此篇文档是老早就放到草稿箱中了,断断续续编辑了几次.结合自己经常用到的对于文件的操作,结合msdn做此文. 1.File 类 --转自msdn 常用方法: 名称 说明 AppendAllLines(S ...
- JavaScript中的this -- 好像很有道理版
函数调用 首先需要从函数的调用开始讲起. JS(ES5)里面有三种函数调用形式: func(p1, p2) obj.child.method(p1, p2) func.call(context, p1 ...
- xshell访问内网虚拟机
1 关闭虚拟机防火墙 chkconfig iptables off 2 查看VMware Network Adapter VMnet8的ip地址 3 虚拟机nat中设置端口转发,抓发至虚拟机内Linu ...
- page.isvalid
背景 看到这个标题,想了半天,为啥用的.应该是当初前台要动态增加行这个事情,当初用.net真是用傻了,竟然对html.aspx原理不大清楚,对于html也想着后台生成.真是弱智啊.谈到这里,想到c#这 ...
- web@HTML重要标签详介绍.
1.table标签<table border="1px " rules="groups" cellpadding="5px" cell ...
- FS 日志空间限定
一.说明: FS默认安装的log文件,仅仅的限制了每个文件的大小,但是没有限制文件的个数.这种情况下,在FS运行很长时间之后,会出现物理空间不够的情况,导致FS或者mysql 或者其他应用没有空间使用 ...
- Centos 安装 Nginx 详细过程
系统 Centos 64位 第一步,首先下载Nginx的tar包及安装依赖的工具tar包. Nginx: http://nginx.org/en/download.html Nginx需要依赖下面3个 ...
- Tomcat 部署项目的三种方法(转)
转自:https://www.cnblogs.com/ysocean/p/6893446.html#_label0 1.下载 Tomcat 服务器 ①.官网下载地址:http://tomcat.apa ...
- django-form介绍
Django form表单 目录 普通方式手写注册功能 views.py login.html 使用form组件实现注册功能 views.py login2.html 常用字段与插件 initia ...