编译你的第一个model

var xxSchema = new Schema({name: 'string', size: 'string'});

var Tank = mongoose.model('Tank', schema);

构造document

document是model的实例. 创建更新document到数据很容易

var Tank = mongoose.model('Tank', tankSchema);

var small = new Tank({size:'small'});
small.save(function(err){
if(err)return handlerError(err);
// saved
}); //or Tank.create({size: 'small'}, function(err, small){
if(err) return handlerError(err);
//saved
}}

查询

model集成了几个内置静态查询方法, 如 find, findById, findOne, where

Tank.find({size: "small"}).where("createdDate").gt(oneYearAgo).exec(callback);

删除

model集成了静态remove方法

Tank.remove({size:'large'}, function(err){
if(err) return handleError(err);
//removed;
}),

更新

每个model都有自己的update方法 这个方法只更新而不返回model, 如果你想更新后返回model使用 findOneAndUpdate

[译]Mongoose指南 - Model的更多相关文章

  1. [译]Mongoose指南 - Population

    MongoDB没有join, 但是有的时候我们需要引用其它collection的documents, 这个时候就需要populate了. 我们可以populate单个document, 多个docum ...

  2. [译]Mongoose指南 - Schema

    定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的表结构. var mongoose = require('mongoose'); ...

  3. [译]Mongoose指南 - 验证

    开始前记住下面几点 Validation定义在SchemaType中 Validation是一个内部的中间件 当document要save前会发生验证 验证不会发生在空值上 除非对应的字段加上了 re ...

  4. [译]Mongoose指南 - 查询

    查询有带callback和不带callback两种方式 所有mongoose的callback都是这种格式: callback(err, result) var Person = mongoose.m ...

  5. [译]Mongoose指南 - Connection

    使用mongoose.connect()方法创建连接 mongoose.conect('mongodb://localhost/myapp'); 上面的代码是通过默认端口27017链接到mongodb ...

  6. [译]Mongoose指南 - 中间件

    中间件是一些函数, 当document发生init, validate, save和remove方法的时候中间件发生. 中间件都是document级别的不是model级别的. 下面讲讲两种中间件pre ...

  7. [译]Mongoose指南 - Plugin

    Schema支持插件, 这样你就可以扩展一些额功能了 下面的例子是当document save的时候自定更新最后修改日期的出插件 // lastMod.js module.exports = expo ...

  8. [译]Mongoose指南 - Document

    更新 有几种方式更新document. 先看一下传统的更新方法 Tank.findById(id, function(err, tank){ if(err) return handleError(er ...

  9. mongoose 的 model,query:增删改查

    简介 mongoose是node.js的一个操作mongodb的模块,比起之前mongodb模块,只需要在开始时连接,不需要手动关闭,十分方便. 连接mongodb 首先你需要安装mongodb.有了 ...

随机推荐

  1. Android程序设计-简单手机通讯录

    在微信中,手机QQ中你会发现软件读取手机通讯录这个功能,这个功能使得软件更好的与手机联系人绑定,从而达到分享,拨打电话,读取用户信息等操作.下面我们将通过一个demo实现这个功能 首先我们看一下效果图 ...

  2. ecshop /includes/lib_base.php、/includes/fckeditor/editor/dialog/fck_spellerpages/spellerpages/server-scripts/spellchecker.php Backdoor Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 ECShop是国内一款流行的网店管理系统软件,其2.7.3版本某个补丁存 ...

  3. Linux Overflow Vulnerability General Hardened Defense Technology、Grsecurity/PaX

    Catalog . Linux attack vector . Grsecurity/PaX . Hardened toolchain . Default addition of the Stack ...

  4. 判断请求是不是ajax

    public static bool IsAjaxRequest(HttpRequest request) { if (request == null) { throw new ArgumentNul ...

  5. 一个关于AdaBoost算法的简单证明

    下载本文PDF格式(Academia.edu) 本文给出了机器学习中AdaBoost算法的一个简单初等证明,需要使用的数学工具为微积分-1. Adaboost is a powerful algori ...

  6. HDU 3466 Proud Merchants(01背包问题)

    题目链接: 传送门 Proud Merchants Time Limit: 1000MS     Memory Limit: 65536K Description Recently, iSea wen ...

  7. sscanf()函数的使用及其实例

    资料引自: 传送门 sscanf函数原型: Int sscanf( const char * src, const char * format, ...); int scanf( const char ...

  8. 【项目】Http请求在NSMutableURLRequest添加HttpBody的字典params属性

    在请求头中加入字典集合的Body,首先把字典转换成json,然后json转换成NSData,然后加入到HTTPBody中,我有已下写法 // 参数paramsNSDictionary * params ...

  9. Nginx系列3之Nginx+tomcat

    preface 公司部分应用跑得的tomcat,众所周知,tcomat高并发性能很弱,所以在处理静态请求的时候,我们就把他抛给Nginx处理,而Tomcat专门处理动态请求.所以在这里说说Nginx+ ...

  10. join的理解

    thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. t.join( ...