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') ...
随机推荐
- python的PIL模块安装
一.Centos安装PIL #尤其重要,否则会报错 yum install python-devel yum install libjpeg libjpeg-devel zlib zlib-devel ...
- 使用新浪IP库获取IP详细地址
使用新浪IP库获取IP详细地址 <?php class Tool{ /** * 获取IP的归属地( 新浪IP库 ) * * @param $ip String IP地址:112.65.102.1 ...
- PAT 天梯赛 L1-024. 后天 【取余】
题目链接 https://www.patest.cn/contests/gplt/L1-024 题意 给出一个数,表示星期几,输出后天是星期几 思路 取余的时候要小心点 AC代码 #include & ...
- UI控件之UITabBarController
UITabBarController:标签栏控制器,继承自UIViewController,用标签栏的形式管理视图控制器,各标签栏对应的视图控制器之间相互独立,互不影响,单击标签栏,显示标签栏对应的视 ...
- DevOps能力是落地微服务的前提
在软件开发领域不存在银弹,当用一项新的技术或新的架构时一定要明白其背后的原理,确保把合适的技术应用在合适的项目上,而不是盲目跟风. 单体应用伸缩性差,而且随着应用规模的扩大,业务逻辑和开发部署过程都变 ...
- MFC输出调试信息
刚学mfc时只知道用MessageBox输出,可是MessageBox只能输出字符串, 对于习惯于printf的我来说非常不便,后来查了一下mfc可以像printf一样输出, 就是TRACE这个宏,用 ...
- GZDBHelper
NuGet:GZDBHelper 初始化: public class APIBase : ApiController { protected GZDBHelper.IDatabase db; publ ...
- 移植opencv2.4.9到android过程记录
http://blog.csdn.net/brightming/article/details/50606463 在移植到arm开发板的时候已经说过,OpenCV已经为各平台准备了一套cmake交叉编 ...
- 测试连接oracle数据库耗时
maven项目 主程序:ConnOracle.java package org.guangsoft.oracle; import java.sql.Connection; import java.sq ...
- Windows10在待机状态时会卡屏的解决方案
问题:Windows10在待机时,会出现卡屏(鼠标.键盘都无法操作,只能重启电脑),区别于平时我们看得比较多的花屏.蓝屏.黑屏. 原因:经过一段时间的待机,Windows10会进入到降电节能模式,由于 ...