Node.Js and Mongoose
Mongoose官方API,我做完之后整理出来的心得。
ONE· Getting Started
First be sure you have MongoDB and Node.js installed.
Next install Mongoose from the command line using npm:
$ npm install mongoose

项目中添加 mongoose模块
Now say we like fuzzy kittens and want to record every kitten we ever meet in MongoDB. The first thing we need to do is include mongoose in our project and open a connection to the test database on our locally running instance of MongoDB.
// getting-started.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

这里面运行官方的代码会有警告,查看原因之后,我进行了如下修改
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/test',{useMongoClient:true});
We have a pending connection to the test database running on localhost. We now need to get notified if we connect successfully or if a connection error occurs:
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
这里要注意,必须先开通mongodb的服务,node才能通过mongoose联通db
With Mongoose, everything is derived from a Schema. Let's get a reference to it and define our kittens.
var kittySchema = mongoose.Schema({
name: String
});
So far so good. We've got a schema with one property, name, which will be a String. The next step is compiling our schema into a Model.
var Kitten = mongoose.model('Kitten', kittySchema);
A model is a class with which we construct documents. In this case, each document will be a kitten with properties and behaviors as declared in our schema. Let's create a kitten document representing the little guy we just met on the sidewalk outside:
var silence = new Kitten({ name: 'Silence' });
console.log(silence.name); // 'Silence'
Kittens can meow, so let's take a look at how to add "speak" functionality to our documents:
// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
}
var Kitten = mongoose.model('Kitten', kittySchema);
Functions added to the methods property of a schema get compiled into the Model prototype and exposed on each document instance:
var fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak(); // "Meow name is fluffy"
We have talking kittens! But we still haven't saved anything to MongoDB. Each document can be saved to the database by calling its save method. The first argument to the callback will be an error if any occured.
fluffy.save(function (err, fluffy) {
if (err) return console.error(err);
fluffy.speak();
});

已经存进去了!
Say time goes by and we want to display all the kittens we've seen. We can access all of the kitten documents through our Kitten model.
Kitten.find(function (err, kittens) {
if (err) return console.error(err);
console.log(kittens);
})
We just logged all of the kittens in our db to the console. If we want to filter our kittens by name, Mongoose supports MongoDBs rich querying syntax.
Kitten.find({ name: /^fluff/ }, callback);
这里面用的是正则,也可以使用指定字段!
This performs a search for all documents with a name property that begins with "Fluff" and returns the result as an array of kittens to the callback.
Congratulations
That's the end of our quick start. We created a schema, added a custom document method, saved and queried kittens in MongoDB using Mongoose. Head over to the guide, or API docs for more.
TWO· 这个例子最新版本出现的警告和解决方案
第一个
https://segmentfault.com/q/1010000010061553/a-1020000010070929
mongoose.connect('mongodb://localhost/test',{useMongoClient:true});
第二个
http://blog.csdn.net/fd214333890/article/details/53486862
mongoose.Promise = global.Promise;
Node.Js and Mongoose的更多相关文章
- [js高手之路]Node.js+jade+mongoose实战todolist(分页,ajax编辑,删除)
该系列文章索引: [js高手之路]node js系列课程-创建简易web服务器与文件读写 [js高手之路]node js系列课程-图解express+supervisor+ejs用法 [js高手之路] ...
- 基于Node.js + jade + Mongoose 模仿gokk.tv
原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 关于gokk 大学的娱乐活动基本就是在寝室看电影了→_→,一般都会选择去goxiazai.cc上看,里面的资源多,质量高 ...
- 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+mongoose+mongodb的开发笔记
时间过得很快,6月和7月忙的不可开交,糟心的事儿也是不少,杭州大连来回飞,也是呵呵. 希望下个阶段能沉浸下来,接着学自己想学的.记一下上几周用了几天时间写的课设.因为课设的缘故,所以在短时间里了解下e ...
- Node.js 常用Mongoose方法
Node.js 手册查询-Mongoose 方法 一.Schema 一种以文件形式存储的数据库模型骨架,无法直接通往数据库端,也就是说它不具备对数据库的操作能力.可以说是数据属性模型(传统意义的表结构 ...
- node.js+express+mongoose实现用户增删查改案例
node.js+express+mongodb对用户进行增删查改 一.用到的相关技术 使用 Node.js 的 express 框架搭建web服务 使用 express 中间件 body-parse ...
- Node.js使用Mongoose包操作MongoDB数据库
1. 安装Mongoose npm install mongoose 2. 使用 2.1 创建连接 var mongoose = require('mongoose'); mongoose.conne ...
- node.js下mongoose简单操作实例
Mongoose API : http://mongoosejs.com/docs/api.html // mongoose 链接var mongoose = require('mongoose'); ...
- [转] node.js下mongoose简单操作实例
Mongoose API : http://mongoosejs.com/docs/api.html // mongoose 链接 var mongoose = require('mongoose') ...
随机推荐
- jQuery:自学笔记(2)——jQuery选择器
jQuery:自学笔记(2)——jQuery选择器 基本选择器 说明 jQuery的基本选择器与CSS的选择器相似: 实例 标签选择器 //使用标签选择器更改字体大小 $(div).css('font ...
- $Android AlarmManager的用法详解
在Android的Alarm机制中,使用AlarmManager可以实现类似闹钟这样的定时任务.在毕业设计项目中要实现定时任务的功能,所以在这里先进行一下梳理. (一)AlarmManager与Bro ...
- Linux基本命令 vim命令(二)
Linux Vim显示行号 在命令模式下输入" : " 进入编辑模式后执行 set nu 命令 即可显示每一行的行号,如果想要取消行号,则再次输入":set nonu&q ...
- Django组件 用户认证,form
auth模块 在进行用户登陆验证的时候,如果是自己写代码,就必须要先查询数据库,看用户输入的用户名是否存在于数据库中: 如果用户存在于数据库中,然后再验证用户输入的密码,这样一来就要自己编写大量的代码 ...
- Python编程-编码、变量、数据类型
一.Python和其他语言对比 C语言最接近机器语言,因此运行效率是最高的,但需要编译. JAVA更适合企业应用. PHP适合WEB页面应用. PYTHON语言更加简洁,丰富的类库,使初学者更易实现应 ...
- bootstrap 模态框中弹出层 input不能获得焦点且不可编辑
bootstrap 模态框中弹出层 input不能获得焦点且不可编辑 问题描述:bs框架支持一层model层的情况下,在模态框中弹出了自定义的弹出层.发现自定义弹出层的输入框不能获得焦点且不可编辑. ...
- Android电容屏(二):驱动调试分析【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/7833383 以goodix的gt8105为例 一.总体架构 硬件部分:先看一个总体的图 ...
- mongodb count 导致不正确的数量(mongodb count 一个坑)
在mongodb 集群中,if 存在orphaned documents 和chunk migration, count查询可能会导致一个不正确的查询结果,例如我就是踩的这个坑,先不说话,看结果: ...
- Flume-NG启动过程源码分析(一)(原创)
从bin/flume 这个shell脚本可以看到Flume的起始于org.apache.flume.node.Application类,这是flume的main函数所在. main方法首先会先解析sh ...
- Asp.net WebAPI 使用流下载文件注意事项
public HttpResponseMessage Post(string version, string environment, string filetype) { var path = @& ...