[译]Mongoose指南 - Model
编译你的第一个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的更多相关文章
- [译]Mongoose指南 - Population
MongoDB没有join, 但是有的时候我们需要引用其它collection的documents, 这个时候就需要populate了. 我们可以populate单个document, 多个docum ...
- [译]Mongoose指南 - Schema
定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的表结构. var mongoose = require('mongoose'); ...
- [译]Mongoose指南 - 验证
开始前记住下面几点 Validation定义在SchemaType中 Validation是一个内部的中间件 当document要save前会发生验证 验证不会发生在空值上 除非对应的字段加上了 re ...
- [译]Mongoose指南 - 查询
查询有带callback和不带callback两种方式 所有mongoose的callback都是这种格式: callback(err, result) var Person = mongoose.m ...
- [译]Mongoose指南 - Connection
使用mongoose.connect()方法创建连接 mongoose.conect('mongodb://localhost/myapp'); 上面的代码是通过默认端口27017链接到mongodb ...
- [译]Mongoose指南 - 中间件
中间件是一些函数, 当document发生init, validate, save和remove方法的时候中间件发生. 中间件都是document级别的不是model级别的. 下面讲讲两种中间件pre ...
- [译]Mongoose指南 - Plugin
Schema支持插件, 这样你就可以扩展一些额功能了 下面的例子是当document save的时候自定更新最后修改日期的出插件 // lastMod.js module.exports = expo ...
- [译]Mongoose指南 - Document
更新 有几种方式更新document. 先看一下传统的更新方法 Tank.findById(id, function(err, tank){ if(err) return handleError(er ...
- mongoose 的 model,query:增删改查
简介 mongoose是node.js的一个操作mongodb的模块,比起之前mongodb模块,只需要在开始时连接,不需要手动关闭,十分方便. 连接mongodb 首先你需要安装mongodb.有了 ...
随机推荐
- clickheat简介
装了个wappalyzer,各种感兴趣的去翻各种网站都用了什么框架啊啊啊...然后在qunar.com遇到了clickheat.之前只是听过这类插件,没想到真的在用唉. ClickHeat is a ...
- wrapper for lua
考虑使用已经有的dll,要写wrapper,使得在lua中能调用dll里的函数,嗯,参考<Programming in lua>,然后仿写luars232. 一.函数定义 先分析一个函数的 ...
- jqGrid使用方法
1.下载文件 下载jqGrid的软件包,下载地址为:http://www.trirand.com/blog/?page_id=6 下载jQuery文件,下载地址为:http://code.jquery ...
- WPF控件ComboBox 每个Item的ToolTip引发的异常
介绍 首先介绍下要实现的任务.做一个下拉框,当选择每个项的时候将鼠标发在上面显示该项的ToolTip的内容(Image). 实现 Model: public class SkinInfo : Noti ...
- 高性能JavaScript笔记一(加载和执行、数据访问、DOM编程)
写在前面 好的书,可能你第一遍并不能领会里面的精魂,当再次细细品评的时候,发现领悟的又是一层新的含义 (这段时间,工作上也不会像从前一样做起来毫不费力,开始有了新的挑战,现在的老大让我既佩服又嫉妒,但 ...
- python click module for command line interface
Click Module(一) ----xiaojikuaipao The following mat ...
- hibernate----(Hql)查询
package com.etc.test; import java.util.List;import java.util.Properties; import org.hibernate.Query; ...
- js 递归下的循环
的递归下的循环不能使用forEach 可以使用for代替 错误写法 // 获取完整名字 var getFullName = function(code, resultName) { if (code ...
- 【原】js检测移动端横竖屏
摘要:上周做了一个小项目,但是要放到我们的app上,然而需要横竖屏使用不同的样式.横屏一套,竖屏一套.调用了手机APP那里的api,可是他们那里ios和安卓返回的不一样. 各种头疼.于是用了css3的 ...
- php适配器设计模式
<?php //适配器模式 //服务器端代码 class tianqi{ public static function show(){ $today= array('tep' =>28 , ...