参考:博客 https://www.cnblogs.com/chentianwei/p/10268346.html

参考: mongoose官网(https://mongoosejs.com/docs/models.html)

参考: 英文:Boosting Node.js和MongoDB with Mongoose


简介:mongoose

Mongoose is a fully developed object document mapping (ODM) library for Node.js and MongoDB.

ODM的概念对应sql的ORM,就是ruby on rails中的activerecord那因层。

activerecord包括migrations, Validations, associations, Query interface, 对应mvc框架中的Models。

ORM, Object-Relational Mappiing。

ODM的作用,定义数据库的数据格式schema, 然后通过它取数据,把数据库中的document映射成程序中的一个对象。这个对象有save, update的系列方法,有tilte, author等系列属性。

在调用这些方法时,odm会根据你调用时使用的条件,转化成mongoDb Shell语言,帮你发送出去。

自然,在程序内使用链式调用,比手写数据库语句更灵活也方便。

例子:

  1. //先安装好MongoDb和Node.js
  2. $ npm install mongoose
  3.  
  4. // getting-started.js
  5. var mongoose = require('mongoose');
  6. mongoose.connect('mongodb://localhost:27017/test');
  7.  
  8. db.on('error', console.error.bind(console, "connection error"))
  9.  
  10. db.once('open', function() {
  11. //当连接成功后,写Schema, model, 写实例并保存到数据库。
  12. })

在db.once内的例子1

  1. var userSchema = new mongoose.Schema({
  2. user: {
  3. username: String,
  4. password: String
  5. }
  6. })
  7.  
  8. var User = mongoose.model('user', userSchema)
  9. var frank = new User({
  10. user: {
  11. username: 'Frank',
  12. password: '123456'
  13. }
  14. })
  15.  
  16. frank.save((err, frank) => {
  17. console.log('save success!')
  18. console.log(frank.user)
  19. }) 

在db.once()的例子2

  1. //构建一个Schema
  2. var kittySchema = new mongoose.Schema({
  3. name: String
  4. });
  5. // 写一个方法
  6. kittySchema.methods.speak = function () {
  7. var greeting = this.name
  8. ? "Meow name is " + this.name
  9. : "I don't have a name";
  10. console.log(greeting);
  11. }
  12. // 生成一个model
  13. var Kitten = mongoose.model('Kitten', kittySchema);
  14. // 实例化一个对象
  15. var fluffy = new Kitten({ name: 'fluffy' });
  16. // 通过mongoose写入数据库
  17. fluffy.save((err, fluffy) => {
  18. if (err) {
  19. return console.error(err)
  20. }
  21. fluffy.speak()
  22. })

⚠️:此时已经将fluffy对象保存到mongodb://localhost:27017/test的Kitten model内。

即将一个document,保存到test数据库的kittens collection中。

model自动创建了kittens这个collection。(自动添加了s)

⚠️注意:此时mongoDb还没有创建kittens

在创建一个实例并执行save方法,test数据库才会创建了kittens collections和documents。

可以对比使用node.js mongodb driver的代码。

  1. var MongoClient = require('mongodb').MongoClient,
  2. assert=require('assert');
  3. var url = 'mongodb://localhost:27017/myproject';
  4. MongoClient.connect(url,function(err,db){
  5. assert.equal(null,err);
  6. console.log("成功连接到服务器");
  7. insertDocuments(db,function(){
  8. db.close();
  9. });
  10. // db.close();
  11. });
  12. var insertDocuments = function(db,callback){
  13. var collection = db.collection('documents');
  14. collection.insertMany([
  15. {a:1},
  16. {a:2},
  17. {a:3}
  18. ],function(err,result){
  19. assert.equal(err,null);
  20. assert.equal(3,result.result.n);
  21. assert.equal(3,result.ops.length);
  22. console.log("成功插入3个文档到集合!");
  23. callback(result);
  1. });
  2. } 

上面代码是专为Node.js提供的驱动程序代码和mongDB shell语言类似。

而,用mongoose定位于使用关系型的数据结构schema,来构造你的app data。

它包括内置的类型构件, 验证, 查询,业务逻辑勾子和更多的功能,开箱即用out of the box!

mongoose把你使用Node.js驱动代码自己写复杂的验证,和逻辑业务的麻烦,简单化了。

mongoose建立在MongoDB driver之上,让程序员可以model 化数据。

二者各有优缺点:

mongoose需要一段时间的学习和理解。在处理某些特别复杂的schema时,会遇到一些限制。

但直接使用Node.js的驱动代码,在你进行数据验证时会写大量的代码,而且会忽视一些安全问题。



Node.js practical 第七章

不喜欢使用mongoose进行复杂的query,而是使用native driver。

Mongoose的缺点是某些查询的速度较慢。

当然Mongoose的优点很多。因为ODM(object document mapping)是现代软件编程的重要部分!

特别是企业级的engineering。

主要优势,就是从database中,提取每件事:程序代码只和object和它们的methods交互。

ODM允许指定:不同类型的对象和把业务逻辑放在类内(和那些对象相关)之间的关系relationships.

另外,内建的验证和类型type casting可以扩展和客制。

当Mongoose和Express.js一起使用时, Mongoose让stack真正地拥护MVC理念。

Mongoose 使用类似Mongo shell, native MongoDB driver的交互方式。

Buckle up!本章将要讨论:

  • Mongoose installation
  • Connection establishment in a standalone Mongoose script
  • Mongoose schemas
  • Hooks for keeping code organized
  • Custom static and instance methods
  • Mongoose models
  • Relationships and joins with population
  • Nested documents
  • Virtual fields
  • Schema type behavior amendment
  • Express.js + Mongoose = true MVC

安装

  1. var mongoose = require('mongoose');
  2. mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true});
  3. //一个mongoose连接实例
  4. var db = mongoose.connection;
  5.  
  6. db.once('open', () => {
  7. //...
  8. })

和native driver不一样,我们无需等待established connection, 只需要把所有的代码放入open()回调内。

不放入open()也可以,默认使用buffer。使用open(),确保连接了服务器。

⚠️官方文档原文的解释:

Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB.

无论是否连接上服务器的MongoDB数据库,都可以马上使用model。

  1. mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true});
    var Schema = mongoose.Schema
  2. var MyModel = mongoose.model('Test', new Schema({ name: String }));
  3. // Works
  4. MyModel.findOne(function(error, result) { /* ... */ });

That's because mongoose buffers model function calls internally. This buffering is convenient, but also a common source of confusion. Mongoose will not throw any errors by default if you use a model without connecting.

这是因为mongoose内部地缓冲了模型函数调用。这个缓冲非常的方便,但也是一个常见的source困惑。

因为如果在没有连接的情况下,你使用model,Mongoose默认不会抛出❌,

  1. //一个脚本
  2. const mongoose = require('mongoose')
  3.  
  4. var MyModel = mongoose.model('Test', new Schema({ name: String}));
  5. //查询的代码会挂起来,指定mongoose成功的连接上。
  6. MyModel.findOne(function(error, result) { /*...*/});
  7.  
  8. setTimeout(function() {
  9. mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true})
  10. }, 6000)

在一个mongoose脚本建立一个连接

连接的URI结构:(一个string)

  1. mongodb://username:password@host:port/database_name

默认可以如下使用,host是localhost, port是27017, 数据库名字是test, 不设置username和password:

  1. mongoose.connect('mongodb://localhost:27017/test', {useMongoClient: true})
  1. mongoose.Promise = global.Promise

Promise这行让mongoose可以使用native ES6 promise 。也可以使用其他的promise implementation 。

  1. Mongoose.prototype.Promise //The Mongoose Promise constructor。

Options对象

connect(url, options)。 options是一个对象,里面是关于连接的属性设置。具体见官方文档。完全支持原生Node.js driver。

Model

下一步: 一个重要的差别(不同于Mongoskin和其他轻量型MongoDB库):

创建一个model, 使用model()函数并传递一个string和一个schema

  1. const Book = mongoose.model("Book", {name: String})

⚠️这里没有使用new mongoose.Schema()

现在配置语句结束,我们创建a document代表Book model 的实例:

  1. const oneBook = new Book({name: 'Practical Node.js'})

Mongoose documents有非常方便的内置方法:validate, isNew, update

(https://mongoosejs.com/docs/api.html#Document)

⚠️留心这些方法只能用在document上,不能用在collection或model上。

docuement是a model的实例, 而a model有点抽象,类似real MongoDB collection。

但是, 它由一个schema支持, 并且作为一个Node.js class(及额外的方法和属性)存在。

Models are fancy constructors compiled from Schema definitions.

通常,我们不直接地使用Mongoose collections, 我们只通过models操作数据。

一些主要的model方法和native MongDB driver类似: find(), insert(), save()等等。

为了把一个docuemnt存入数据库,使用document.save()

这个方法是异步的asynchronous。因此添加一个callback或者promise或者async/await函数。

执行下面的脚本代码⚠️先打开MongoDB,server。

  1. const mongoose = require('mongoose')
  2. mongoose.connect('mongodb://localhost:27017/test')
  3. mongoose.Promise = global.Promise
  4. const Book = mongoose.model("Book", {name: String})
  5.  
  6. const oneBook = new Book({name: "Hello world!"})
  7.  
  8. oneBook.save((err, result) => {
  9. if (err) {
  10. console.err(err)
  11. process.exit(1)
  12. } else {
  13. console.log("Saved:", result)
  14. process.exit(0)
  15. }
  16. })

Mongoose Schemas

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

Mongoose开始于一个schema. 每个scheme映射到一个MongoDB collection并定义这个collection中的document的外形。

  1. var mongoose = require('mongoose');
  2. var blogSchema = new mongoose.Schema({
  3. title: String,
  4. comments: [{body: String, date: Date}],
  5. date: { type: Date, default: Date.now},
  6. hidden: Boolean
  7. })
  8.  
  9. //add()方法,用于添加属性,参数是一个key/value对象, 或者是另一个Schema.
  10. //add()可以链式调用。
  11. blogSchema.add({author: String})

每个key在我们的documents内定义了一个属性并给予一个相关的SchemaType。

key也可以是嵌套的对象。

SchemaTypes:

  • String, Number, Date, Boolean
  • Buffer: a Node.js binary type(图片images, PDFs, archives等等)
  • Mixed: 一种"anything goes"类型。任意类型的数据
  • ObjectId: _id key 的类型。
  • Array
  • map

Schema不只定义document的结构和属性,也定义document的实例方法,静态Model方法, 混合的compond indexes, 文档hooks 调用middleware。

创建model

为了使用我们的schema定义,需要转化blogSchema进入一个Model:

  1. var Blog = mongoose.model('Blog', blogSchema)

Models的实例是documents。Documents有内建的实例方法。

Instance methods

通过schema定义客制化的实例方法:

  1. var animalSchema = new Schema({ name: String, type: String })
  2.  
  3. // 分配一个函数给methods对象
  4. animalSchema.methods.findSimilarTypes = function(callback) {
  5. return this.model("Animal").find({ type: this.type }, callback)
  6. }
  1. var Animal = mongoose.model('Animal', animalSchema)
  2. var dog = new Animal({type: 'dog'})
  3. // 存入数据库
    dog.save((err, dog) => {
  4. console.log("save success!")
  5. })
  6. // dog document使用自定义的方法
  7. dog.findSimilarTypes(function(err, dogs) {
  8. console.log("yes", dogs); // yes [ { _id: 5c45ba13aaa2f74d3b624619, type: 'dog', __v: 0 } ]
  9. });

Statics

给一个Model增加一个静态方法。

把一个函数分配给animalSchema的statics对象。

如果把一个Model看成一个类,那么静态方法就是这个类的类方法。

  1. animalSchema.statics.findByName = function(name, callback) {
  2. return this.find({name: new RegExp(name, "i") }, callback)
  3. }
  4.  
  5. var Animal = mongoose.model("Aniaml", animalSchema)
  6. Animal.findByName("fido", function(err, animals) {
  7. console.log("result: ", animals)
  8. }) 

⚠️,声明statics,不能使用箭头函数。因为箭头函数明确地防止绑定this。

也可以使用Schema.static(name, funtion)方法

  1. var schema = new mongoose.Schema(..);
  2.  
  3. schema.static('findByName', function(name, callback) => {
  4. return this.find({name: name}, callback)
  5. })

使用{name: fn, name:fun, ...}作为唯一参数:

如果把一个hash(内含多个name/fn 对儿),作为唯一的参数传递给static(), 那么每个name/fn对儿将会被增加为statics静态方法。

  1. bookSchema.static({ // Static methods for generic, not instance/document specific logic
  2. getZeroInventoryReport: function(callback) {
  3. // Run a query on all books and get the ones with zero inventory
  4. // Document/instance methods would not work on "this"
  5. return callback(books)
  6. },
  7. getCountOfBooksById: function(bookId, callback){
  8. // Run a query and get the number of books left for a given book
  9. // Document/instance methods would not work on "this"
  10. return callback(count)
  11. }
  12. })

Query Helpers

可以增加query helper functions, 类似实例方法(❌?这句不是很明白,哪里类似了?),

但是for mongoose queries。

Query helper methods 让你扩展mongoose的链式查询builder API。chainable query builder API.

  1. animalSchema.query.byName = function(name) {
  2. return this.where({ name: new RegExp(name, 'i') });
  3. };
  4.  
  5. var Animal = mongoose.model('Animal', animalSchema);
  6.  
  7. Animal.find().byName('fido').exec(function(err, animals) {
  8. console.log(animals);
  9. });

⚠️由上可见query helper方法是Model调用的。所以原文 like instance methods 这句不明白。

indexes

MongDB支持第二个indexes.

使用mongoose,定义indexes的方法有2个:

  • 在定义一个Schema时
  • 使用Schema对象的index()方法。(主要用于组合式索引)
  1. var animalSchema = new mongoose.Schema({
  2. name: String,
  3. type: String,
  4. tags: { type: [String], index: true}
  5. })
  6.  
  7. animalSchema.index({ name: 1, type: -1})

Virtuals

document的一个属性。

Options

Schemas有一些选项配置,可以用构建起或者用set()

  1. new mongoose.Schema({..}, options)
  2.  
  3. // or
  4. var schema = new mongoose.Schema({..})
  5. schema.set(option, value)

Pluggable

Mongoose schemas是插件方式的, 即可以通过其他程序的schemas进行扩展。

(具体使用点击连接)

Hooks for Keeping Code Organized

假如:在有大量关联的对象的复杂应用内,我们想要在保存一个对象前,执行一段逻辑。

使用hook,来储存这段逻辑代码是一个好方法。例如,我们想要在保存一个book document前上传一个PDF到web site:

  1. //在一个schema上使用pre()钩子:
    booSchema.pre('save', (next) => {
  2. // Prepare for saving
  3. // Upload PFD
  4. return next()
  5. })

pre(method, [options], callback)

第一个参数是method的名字

⚠️:钩子和方法都必须添加到schemas上,在编译他们到models 之前。也就是说,在调用mongoose.model()之前。


官方guide: SchemaTypes摘要

SchemaTypes处理definition of path defaults , 验证, getterssetters,  查询的默认field selection, 和Mongoose document属性的其他一些普遍特征。

你可以把一个Mongoose Schema看作是Mongoose model的配置对象。

于是,一个SchemaType是一个配置对象,作为一个独立的属性。

  1. const schema = new Schema({ name: String });
  2. schema.path('name') instanceof mongoose.SchemaType; // true
  3. schema.path('name') instanceof mongoose.Schema.Types.String; // true
  4. schema.path('name').instance; // 'String'
  1. // 一个userSchema的userSchema.path("name"):
  2. SchemaString {
  3. enumValues: [],
  4. regExp: null,
  5. path: 'name',
  6. instance: 'String',
  7. validators: [],
  8. getters: [],
  9. setters: [],
  10. options: { type: [Function: String] },
  11. _index: null }

我觉得:一个path类似关系型数据库中的table中的一个field定义。

所以一个SchemaType,表达了一个path的数据类型, 它是否是getters/setters的模式。

一个SchemaType不等于一个Type。它只是Mongoose的一个配置对象。

  1. mongoose.ObjectId !== mongoose.Types.ObjectId

它只是在一个schema内,对一个path的配置。

常用的SchemaTyps:

  1. var schema = new mongoose.Schema({
  2. name: String,
  3. binary: Buffer,
  4. living: Boolean,
  5. updated: { type: Date, default: Date.now},
  6. age: { type: Number, min: 18, max: 65},
  7. mixed: Schema.Types.Mixed,
  8. _someId: Schema.Types.ObjectId,
  9. array: []
  10. })

数组的SchemaTypes:

  1. var schema = new Schema({
  2. ofString: [String],
  3. ofNumber: [Number],
  4. ofDates: [Date],
  5. ofBuffer: [Buffer],
  6. ofBoolean: [Boolean],
  7. ofMixed: [Schema.Types.Mixed],
  8. ofObjectId: [Schema.Types.ObjectId],
  9. ofArrays: [[]],
  10. ofArrayOfNumbers: [[Number]],
    //嵌套对象
  11. nested: {
  12. stuff: { type: String, lowercase: true, trim: true}
  13. },
  14. map: Map,
  15. mapOfString: {
  16. type: Map,
  17. of: String
  18. }
  19. })

SchemaType Options:

  1. var schema1 = new Schema({
  2. test: String // `test` is a path of type String
  3. });
  4.  
  5. var schema2 = new Schema({
  6. // The `test` object contains the "SchemaType options"
  7. test: { type: String, lowercase: true } // `test` is a path of type string
  8. });

你可以增加任何属性(你想要给你的SchemaType options)。 有许多插件客制化SchemaType options。

Mongoose有几个内置的SchemaType options(具体见https://mongoosejs.com/docs/schematypes.html)

indexes

可以用schema type options定义MongoDB indexes:

  1. var schema2 = new Schema({
  2. test: {
  3. type: String,
  4. index: true,
  5. unique: true // Unique index. If you specify `unique: true`
  6. // specifying `index: true` is optional if you do `unique: true`
  7. }
  8. });

不同的SchemaType有不同的options,具体见官方guide。


Mongoose Models

正如许多ORMs/ODMs, 在mongoose中,cornerstone object is a model。对象的基石是模块。

把一个schema编译进入一个model, 使用:

mongoose.model(name, schema)

第一个参数name,是一个字符串,大写字母开头,通常这个string和对象字面量(声明的变量名)一样。

默认,Mongoose会使用这个model name的复数形式去绑定到一个collection name。

Models用于创建documents(实际的data)。使用构建器:

new ModelName(data)

Models又内建的静态类方法类似native MongoDB方法,如find(), findOne(), update(), insertMany()

一些常用的model 方法:

  • Model,create(docs) 等同new Model(docs).save()
  • Model.remove(query, [callback(error)])。不能使用hooks。
  • Model.find(query, [fields], [options], [callback(error, docs)])
  • Model.update()
  • Model.populate(docs, options, [callback(err, doc)]), 填入。
  • Model.findOne
  • Model.findById

注意⚠️,一部分model方法不会激活hooks, 比如deleteOne(),remove()。他们会直接地执行。

最常用的实例方法:

  • save()
  • toJSON([option]): 把document转化为JSON
  • toObject(): 把document转化为普通的JavaScript对象。
  • isModified([path]): True/false
  • doc.isNew: True/false
  • doc.id: 返回document id
  • doc.set():参数包括path, val, [type],  ⚠️path其实就是field名字key/value对儿的key。
  • doc.validate(): 手动地检测验证(自动在save()前激活)

大多数时候,你需要从你的document得到数据。

使用res.send()把数据发送到一个客户端。

document对象需要使用toObject()和toJSON()转化格式,然后再发送。


Document

Retrieving

具体见:querying一章。

updating

可以使用findById(), 然后在回调函数内修改查询到的实例的属性值。

  1. Tank.findById(id, function (err, tank) {
  2. if (err) return handleError(err);
  3.  
  4. tank.size = 'large'; //或者使用tank.set({ size: 'large' })
  5. tank.save(function (err, updatedTank) {
  6. if (err) return handleError(err);
  7. res.send(updatedTank);
  8. });
  9. });

如果只是想要把新的数据更新到数据库,不返回,则可以使用Model#updateOne()

  1. Tank.update({_id: id}, { $set: {size: 'large'}}, callback)

如果如findById加上save(),返回新的数据,有更方便的方法: findByIdAndupdate()

配合使用res.send()

  1. Tank.findByIdAndUpdate(id, { $set: { size: 'large' }}, { new: true }, function (err, tank) {
  2. if (err) return handleError(err);
  3. res.send(tank);
  4. });

⚠️,findByIdAndUpdate不会执行hooks或者验证,所以如果需要hooks和full documente validation,用第一种query然后save() it。

Validating

Documents在被保存前需要验证,具体见validation

重写

.set(doc)方法,参数是另一document的话,相当于重写。


Relationships and Joins with Population

使用Model.populate()或者 Query.populate()

虽然,Node开发者不能查询Mongo DB(on complex relationships), 但是通过Mongoose的帮助,开发者可以在application layer做到这点。

在大型的程序中,documents之间又复杂的关系,使用mongoose就变得很方便了。

例如,在一个电子商务网站,一个订单通过产品id,关联产品。为了得到更多的产品信息,开发者需要写2个查询: 一个取order,另一个取订单的产品。

使用一个Mongoose query就能做到上面的2个查询的功能。

Populate

Mongoose通过连接订单和产品让2者的关系变得简单:Mongoose提供的一个功能,population。

这里population涉及的意思类似related,即相关的,有联系的。

populations是关于增加更多的data到你的查询,通过使用relationships。

它允许我们从一个不同的collection取数据来fill填document的一部分。

比如我们有posts和users,2个documents。Users可以写posts。这里有2类方法实现这个写功能:

  1. 使用一个collection,users collection有posts 数组field。这样就只需要一个单独的query,但是这种结构导致某些方法的被限制。因为posts不能被indexed or accessed separately from users.
  2. 或者使用2个collections(and models)。在这个案例,这种结构会更灵活一些。但是需要至少2个查询,如果我们想要取一个user和它的posts。

于是Mongoose提供了population,在这里有用武之地了。

在user schema内引用posts。之后populate这些posts。为了使用populate(), 我们必须定义ref和model的名字:

  1. const mongoose = require('mongoose')
  2.  
  3. const Schema = mongoose.Schema
  4.  
  5. const userSchema = new Schema({
  6. _id: Number,
  7. name: String,
  8. posts: [{
  9. type: Schema.Types.ObjectId,
  10. ref: 'Post'
  11. }]
  12. })

⚠️,Schema.Types.ObjectId是一种SchemaType。

实际的postSchema只加了一行代码:

  1. const postSchema = Schema({
  2. _creator: { type: Number, ref: 'User'},
  3. title: String,
  4. text: String
  5. })

下面的几行代码是我们创建models, 然后yes!!! 只用一个findOne()类方法即可得到全部的posts的数据。

执行exec()来run:

  1. const Post = mongoose.model("Post", postSchema)
  2. const User = mongoose.model('User', userSchema)
  3. //添加一些数据,并存入MongoDB数据库
    User.findOne({name: /azat/i})
  4. .populate('posts')
  5. .exec((err, user) => {
  6. if (err) return handleError(err)
  7. console.log('The user has % post(s)', user.posts.length)
  8. })

⚠️ ObjectIdNumberString, and Buffer are valid data types to use as references,

meaning they will work as foreign keys in the relational DB terminology.

知识点:

  • 正则表达式:找到所有匹配azat的string,大小写敏感, case-insensitively。
  • console.log中的 %, 一种字符串插入符号的写法,把user.posts.length插入这个字符串。

也可以只返回一部分填入的结果。例如,我们能够限制posts的数量为10个:

⚠️在mongoose, path指 定义一个Schema中的type类型的名字

  1. .populate({
  2. path: 'posts',
  3. options: { limit: 10, sort: 'title'}
  4. })

有时候,只会返回指定的fileds,而不是整个document,使用select:

  1. .populate({
  2. path: 'posts',
  3. select: 'title',
  4. options: {
  5. limit: 10,
  6. sort: 'title'
  7. }
  8. })

另外,通过一个query来过滤填入的结果!

  1. .populate({
  2. path: 'posts',
  3. select: '_id title text',
  4. match: {text: /node\.js/i},
  5. options: { limit: 10, sort: '_id'}
  6. })

查询选择的属性使用select, 值是一个字符串,用空格分开每个field name。

建议只查询和填入需要的fields,因为这样可以防止敏感信息的泄漏leakage,降低系统风险。

populate方法可以find()连接使用,即多个document的查询。

问题:

1. user.posts.length,这是user.posts是一个数组吗?所以可以使用length方法。

答:是的,在定义userSchema时,posts field的数据类型是数组。

2.exec()的使用:

Model.find()返回<Query>, 然后使用Query.populate()并返回<Query>this, 然后使用Query.exec()返回Promise

3 type和ref

type代表SchemType。ref属性是SchemaType Options的一种。和type属性配合使用。

4.上面的案例,如何保存有关联的数据?

  1. var user = new User({name: "John", _id: 2})
  2. var post = new Post({title: "New land", text: "Hello World!"})
  3. user.posts = post._id
  4. post._creator = user._id
  5. user.save()
  6. post.save()
  7.  
  8. User.findOne({_id: 2}).populate("posts")
  9. .exec((error, user) => {
  10. console.log(user.posts.length)
  11. })

还需要看官网的Populate一章。讲真,这本书讲的都很浅显,有的没有说清楚。

理解:User和Post各自有一个含有选项ref的path。因此双方建立了关联。


官方guide Populate()

Population是指: 在一个document内,用来自其他collection(s)的document,自动地取代指定paths的值。

我们可以populate一个单独的document,多个documents, 普通的object,多个普通的objects, 或者从一个query返回的所有objects。

基础

  1. const mongoose = require('mongoose')
  2. const Schema = mongoose.Schema
  3. mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true})
  4. // var db = mongoose.connection
  5.  
  6. const personScheam = Schema({
  7. _id: Schema.Types.ObjectId,
  8. name: String
  9. age: Number,
  10. stories: [{ type: Schema.Types.ObjectId, ref: "Story"}]
  11. })
  12.  
  13. const storySchema = Schema({
  14. author: { type: Schema.Types.ObjectId, ref: "Person"},
  15. title: String,
  16. fans: [{ type: Schema.Types.ObjectId, ref: "Person"}]
  17. })
  18.  
  19. const Story = mongoose.model("Story", storySchema)
  20. const Person = mongoose.model("Person", personScheam)

注意⚠️

  • 使用ref选项的path的类型必须是ObjectId, Number, String, Buffer之一。
  • 通常使用ObjectId, 除非你是一个高级用户或有充分如此做的原因

saving refs

保存refs到其他documents和你保存属性的方式一样,指需要分配_id值:

  1. const author = new Person({
  2. _id: new mongoose.Types.ObjectId,
  3. name: "Ian Fleming",
  4. age: 50
  5. })
  6.  
  7. author.save((err) => {
  8. if (err) return handleError(err)
  9.  
  10. const story1 = new Story({
  11. title: "Casino Royale",
  12. author: author._id
  13. })
  14.  
  15. story1.save((err, story1) => {
  16. if (err) return handleError(err)
  17. console.log("Success stores", story1.title)
  18. })
  19. })

上面的代码,因为story1有外键author(即通过_id建立了两个documents的关联), 所以story1能直接populate author的数据。

Population

现在填入story的author,使用query builder:

  1. Story.findOne({ title: "Casino Royale"})
  2. .populate('author')
  3. .exec((err, story) => {
  4. if (err) return handleError(err)
  5. console.log("The author is %s", story.author.name)
  6. })

通过在返回结果前运行一个独立的query,(findOne()方法返回的是一个Query对象)

填入的paths不再是它们的原始的_id, 它们的值被替换为从数据库返回的document。

Arrays of refs和 非Arrays of refs的工作方式一样。都是在query对象上调用populate方法,并返回一个array of documents来替代原始的_ids。

Setting Populated Fields

也可以手动填入一个对象,来替换_id。把一个document对象赋值给author属性。

这个对象必须是你的ref选项所涉及的model的一个实例:

  1. //假设之前已经向数据库存入了一个person和一个story, story有person的外键:
  2. Story.findOne({ title: "Casino Royale"}, (error, story) => {
  3. if (error) {
  4. return handleError(error)
  5. }
  6. Person.findOne({name: "Ian Fleming"}).exec((err, person) => {
  7. story.author = person

  8. console.log(story.author.name)
  9. })
  10. })
    //控制台会输出author的名字

这是不使用populate的方法。和使用populate的效果一样,都是替换掉了_id。

hat If There's No Foreign Document?

Mongoose populate不像传统的SQL joins。类似left join in SQL。

  1. Person.deleteMany({ name: "Ian Fleming" }, (err, result) => {
  2. if (err) {
  3. console.log("err: ",err)
  4. } else {
  5. console.log("res: ", result)
  6. }
  7. });
  8.  
  9. //因为没有了Person中的document, story.author.name是null。
  10. Story.findOne({ title: "Casino Royale"})
  11. .populate('author')
  12. .exec((err, story) => {
  13. if (err) return handleError(err)
  14. console.log("The author is %s", story.author.name)
  15. })

如果storySchema的authors path是数组形式的, 则populate()会返回一个空的array

Field Selection

如果只想从返回的populated documents得到指定的fields, 可以向populate()传入第二个参数: field name\

populate(path, [select])

  1. Story.findOne({ title: "Casino Royale"})
  2. .populate('author', 'name')
  3. .exec((err, story) => {
  4. if (err) return handleError(err)
  5. console.log("The author is %s", story.author.name)
  6. //返回The authors age is undefined
  7. console.log('The authors age is %s', story.author.age)
  8. })

Populating Multiple Paths

如果我们想要同时填入多个paths, 把populate方法连起来:

  1. Story.
  2. find(...).
  3. populate('fans').
  4. populate('author').
  5. exec();

Query conditions and other options

如果我们想要填入populate的fans数组基于他们的age, 同时只选择他们的名字,并返回最多5个fans, 怎么做?

  1. Story.find(...)
  2. .populate({
  3. path: 'fans',
  4. match: {age: { $gte: 21 }},
  5. // 使用"-_id",明确表示不包括"_id"field。
  6. select: "name -_id",
  7. options: { limit: 5}
  8. })
  9. .exec()

Refs to chlidren

本章Populate官网教程提供的案例,auhtor对象的stories field并没有被设置外键。

因此不能使用author.stories得到stories的列表。

这里有2个观点:perspectives:

第一, 你想要author对象知道哪些stories 是他的。通常,你的schema应该解决one-to-many关系,通过在many端加一个父pointer指针。但是,如果你有好的原因想要一个数组的child指针,你可以使用push()方法,把documents推到这个数组上:

  1. author.stories.push(story1)
  2. author.save(callback)

这样,我们就可以执行一个find和populate的联合

  1. Person.
  2. findOne({ name: 'Ian Fleming' }).
  3. populate('stories'). // only works if we pushed refs to children
  4. exec(function (err, person) {
  5. if (err) return handleError(err);
  6. console.log(person);
  7. });

是否真的要设置2个方向的pointers是一个可争论的地方。

第二,作为代替, 我们可以忽略populating,并直接使用find()方法,找到stories:

  1. Story.
  2. find({ author: author._id }).
  3. exec(function (err, stories) {
  4. if (err) return handleError(err);
  5. console.log('The stories are an array: ', stories);
  6. });

Populating an existing document

如果我们有一个正存在的mongoose document并想要填入一些它的paths,

可以使用document#populate() , 返回Document this。

  1. doc.populate(path|options, callback)
  2. // or
  3. doc.populate(options).execPopulate()

Populating multiple existing documents

如果我们有多个documents或者plain objects, 我们想要填入他们,使用Model.populate()方法。

这和document#populate(), query#populate()方式类似。

populate(docs, options, [callback(err, doc)])  返回Promise.

  • docs <Document|Array>,一个单独的对象或者一个数组的对象。
  • options <Object| Array> 一个hash的key/value对儿。可使用的顶级options:
    • path: 值是要填入的path的名字
    • select: 选择要从数据库得到的fields
    • match: 可选的查询条件用于匹配
    • model: 可选的model的名字,用于填入。(已知是用在不同数据库的model实例的填入)
    • options: 可选的查询条件,比如like, limit等等。
    • justOne: 可选的boolean,如果是true,则设置path为一个数组array。默认根据scheam推断。
  1. // populates an array of objects
    // find()返回一个query,里面的result是一个array of documents, 因此opts也应该是一个array of document
  2. User.find(match, function (err, users) {
  3. var opts = [{ path: 'company', match: { x: 1 }, select: 'name' }]
  4.  
  5. var promise = User.populate(users, opts);
  6. promise.then(console.log).end();
  7. })

填入一个object, 和上面填入一个array of objects, 和填入很多plain objects。具体见文档

Populating across multiple levels跨越多层的填入

一个model的内的实例可以互相关联。即Self Joins

(这在Rails中的例子也是自身model上加一个foreign_key)

一个user schema可以跟踪user的朋友:

⚠️,关键使用ref选项,引用"User"自身!!!

  1. var userSchema = new Schema({
  2. name: String,
  3. friends: [{ type: Scheam.Types.ObjectId, ref: 'User'}]
  4. })

Populate让你得到一个user的朋友的列表。

但是如果你也想要一个user的朋友的朋友哪?加一个populate选项的嵌套:

  1. User.
  2. findOne({ name: 'Val' }).
  3. populate({
  4. path: 'friends',
  5. // Get friends of friends - populate the 'friends' array for every friend
  6. populate: { path: 'friends' }
  7. });

一个完整的例子:

  1. //populate.js
  2. const mongoose = require('mongoose')
  3. const Schema = mongoose.Schema
  4. mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true})
  5.  
  6. const userSchema = new Schema({
  7. _id: Number,
  8. name: String,
  9. friends: [{
  10. type: Number,
  11. ref: 'User'
  12. }]
  13. })
  14.  
  15. const User = mongoose.model("User", userSchema)
  16.  
  17. //存入下面的数据
  18. var user = new User({ name: "chen", _id: 3, friends: [4] }).save()
  19. var user2 = new User({ name: "haha", _id: 4, friends: [3, 5] }).save()
  20. var user3 = new User({ name: "ming", _id: 5, friends: [5] }).save()

执行查询,使用populate选项:

  1. User.findOne({_id: 3})
  2. .populate({
  3. path: 'friends',
  4. populate: {path: 'friends'}
  5. })
  6. .exec((err, result) => {
  7. console.log(result)
  8. })
  9. //返回
  10. { posts: [],
  11. friends:
  12. [ { posts: [],
  13. friends:
  14. [ { posts: [], friends: [ 4 ], _id: 3, name: 'chen', __v: 0 },
  15. { posts: [], friends: [ 5 ], _id: 5, name: 'ming', __v: 0 } ],
  16. _id: 4,
  17. name: 'haha',
  18. __v: 0 } ],
  19. _id: 3,
  20. name: 'chen',
  21. __v: 0 }

Populating across Databases跨越数据库的填入

使用model选项

之前的练习:

  1. //引进mongoose
  2. const mongoose = require('mongoose')
  3. //得到Schema构建器
  4. const Schema = mongoose.Schema
  5. //mongoose实例连接到本地端口27017的数据库test
  6. mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true})
  7. //得到connection对象实例, 因为实际的原因,一个Connection等于一个Db
  8. var db = mongoose.connection
  1. // with mongodb:// URI, 创建一个Connection实例
    // 这个connection对象用于创建和检索models。
    // Models总是在一个单一的connection中使用(scoped)。

  2. var db = mongoose.createConnection('mongodb://user:pass@localhost:port/database'); 

假如,events和conversations这2个collection储存在不同的MongoDB instances内。

  1. var eventSchema = new Schema({
  2. name: String,
  3. // The id of the corresponding conversation
  4. // ⚠️没有使用ref
  5. conversation: Schema.Typs.ObjectId
  6. });
  7. var conversationSchema = new Schema({
  8. numMessages: Number
  9. });
  1. var db1 = mongoose.createConnection('localhost:27000/db1');
  2. var db2 = mongoose.createConnection('localhost:27001/db2');
  3. //⚠️,我的电脑上不能同时开2个mongd,提示❌
    exception in initAndListen: DBPathInUse: Unable to lock the lock file: /data/db/mongod.lock (Resource temporarily unavailable). Another mongod instance is already running on the /data/db directory, terminating
  4. var Event = db1.model('Event', eventSchema);
  5. var Conversation = db2.model('Conversation', conversationSchema);

这种情况下,不能正常使用populate()来填入数据,需要告诉populate使用的是哪个model:

  1. Event.
  2. find().
  3. populate({ path: 'conversation', model: Conversation }).
  4. exec(function(error, docs) { /* ... */ });

实践的例子: 跨MongoDB databases实例。

  1. // Populating across Databases
  2. const mongoose = require('mongoose')
  3. const Schema = mongoose.Schema
  4. mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true })
  5. var db2 = mongoose.createConnection('mongodb://localhost:27017/db2', { useNewUrlParser: true })
  6.  
  7. // 创建2个Schema。
  8. var eventSchema = new Schema({
  9. name: String,
  10. conversation: Schema.Types.ObjectId
  11. });
  12. var conversationSchema = new Schema({
  13. numMessages: Number
  14. });

  15. // 在test 数据库上创建一个Event类的实例。
  16. var Event = mongoose.model('Event', eventSchema)
  17. var event = new Event({name: "click"}).save()
  18. // 在db2 数据库上创建一个Conversation类的实例
  19. var Conversation = db2.model('Conversation', conversationSchema);
  20. var conversation = new Conversation({numMessages: 50}).save()
    // 我在mongDb shell中给event document增加了一个field(conversation: XX),值是conversation实例的_id

启动上面的脚本后,我修改脚本去掉创建实例的2行代码,然后添加一个find和populate, 然后重启脚本:

  1. Event.find()
  2. .populate({ path: 'conversation', model: Conversation})
  3. .exec((error, docs) => {
  4. console.log(docs)
  5. })

成功,填入conversation: (这个例子就是在不同database的一对一关联)

  1. [ { _id: 5c4ad1f2916c8325ae15a6ac,
  2. name: 'click',
  3. __v: 0,
  4. conversation: { _id: 5c4ad1f2916c8325ae15a6ad, numMessages: 50, __v: 0 } } ]

上面的练习,

  • 如果在find()内去掉model,再次运行脚本,返回的数组内的conversation field的值是 null
  • 如果在find()内去掉model, 然后在eventSchema内加上ref,再次运行脚本。返回null。

上面的练习,把2个model放在同database下,可以正确运行的✅。

即eventSchema没有使用 ref, 但在find().populate()内使用了model: "Conversation", 可以填入对应的conversation实例。

因为官方文档:Query.prototype.populate()的参数[model]的解释是这样的:

  1. «Model» The model you wish to use for population.
    If not specified, populate will look up the model by the name in the Schema's ref field.

即,

如果populate方法内指定了model选项,则从这个model中找对应的document。

如果没有指定model,才会在eventSchema中找ref选项,因为ref的值就是一个model的名字。

结论就是,不论是model选项还是 ref选项,它们都是把2个document连接起来的辅助。

Dynamic References via refPath

Populate Virtuals

Populate Virtuals: The Count Option

Populate in Middleware


Nested Documents

上一章population。 这是一种传统的方法,来设计你的数据库。它minic模仿了关系型数据库设计并使用普通的forms和严格的数据原子化atomization。

The document storage model in NoSQL databases is well suited to use nested documents。

如果你指定最频繁运行的查询是什么,使用nested documents是更好的选择。

你可以优化你的数据库让它倾向某一个查询。

例如,大多数典型的使用案例是读用户数据。那么代替使用2个collections(posts and users),

我们可以用单一的collections(users), 内部嵌套posts。

绝对使用哪种方式更多的是建筑学的问题,它的答案由具体使用决定。

例如,

  • 如果有类似blog的功能,多个用户会读取作者的posts,就需要独立的查询作者的posts。分开的collection会更好。
  • 如果posts只在作者的个人页面使用,那么最好就用nested documents。

使用Schema.Types.Mixed类型

  1. const userSchema = new mongoose.Schema({
  2. name: String,
  3. posts: [mongoose.Schema.Types.Mixed]
  4. })
  5. // Attach methods, hooks, etc.
  6. const User = mongoose.model('User', userSchema)

更灵活的Schema设计,分成2个Schema:

  1. const postSchema = new mongoose.Schema({
  2. title: String,
  3. text: String
  4. })
  5. // Attach methods, hooks, etc., to post schema
  6. const userSchema = new mongoose.Schema({
  7. name: String,
  8. posts: [postSchema]
  9. })
  10. // Attach methods, hooks, etc., to user schema
  11. const User = mongoose.model('User', userSchema)

增加子文档到arrays:

因为使用了数组,所以可以使用push, unshift, 等方法(在JavaScript/Node.js)或者MongoDB$push操作符号来更新user document:

  1. User.updateOne(
  2. {_id: userId},
  3. {$push: {posts: newPost}},
  4. (error, results) => {
  5. // 处理错误和检测结果
  6. }
  7. )

操作符号有复杂的附加功能,可以处理各种情况

也可以使用save():

  1. var childSchema = new Schema({name: String})
  2.  
  3. var parentSchema = new Schema({
  4. children: [childSchema],
  5. name: String
  6. })
  7.  
  8. var Parent = mongoose.model('Parent', parentSchema)
  9.  
  10. var parent = new Parent({
  11. children: [{name: 'Matt'}, {name: 'Sarah'}]
  12. })
  13. parent.children[0].name = 'Matthew'
    parent.children.push({ name: 'Liesl'})
  14. parent.save((error, result) => {
  15. if (error) return console.log(error)
  16. console.log(result)
  17. })

得到:

  1. { _id: 5c47d630d93ce656805231f8,
  2. children:
  3. [ { _id: 5c47d630d93ce656805231fa, name: 'Matthew' },
  4. { _id: 5c47d630d93ce656805231f9, name: 'Sarah' } ,
    { _id: 5c47d9b07517b756fb125221, name: 'Liesl' } ],
  5. __v: 0 }

注意⚠️,新增了3个child,  和parent一起存在mongoDB的test数据库的parents collections内

查询一个子document

每个子document默认有一个_id。

Mongoose document arrays有一个特别的id方法用于搜索一个doucment array来找到一个有给定_id值的document。

  1. var doc = parent.children.id(_id)

移除使用remove方法,相当于在子文档内使用.pull()

  1. parent.children.pull(_id)
  2. //等同
  3. parent.children.id(_id).remove()

  4. //对于:a single nested subdocument:
  5. parent.child.remove()
  6. //等同
  7. parent.child = null

官方文档Queries

Mongoose models提供用于CRUD操作的静态帮助函数。这些函数返回一个mongoose Query 对象。

  • Model.deleteOne(),  deleteMany()
  • Model.find()
  • Model.findById(), 及衍生出findByIdAndDelete(),  findByIdAndRemove, findByIdAndUpdate
  • Model.findOne(),  及衍生出findOneAndDelete(), findOneAndRemove, findOneAndUpdate
  • Model.replace() ,类似update(), 用传入的doc取代原来的document
  • Model.updateOne(),  Model.updateMany()。

一个Query对象可以使用.then()函数。

query with callback

当使用一个query并传入一个callback(), 你指定你的query作为一个JSON document。

这个JSON document的语法和MongoDB shell相同。

  1. var Person = mongoose.model('Person', yourSchema);
  2.  
  3. // find each person with a last name matching 'Ghost', selecting the `name` and `occupation` fields
  4. Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) {
  5. if (err) return handleError(err);
  6. console.log('%s %s is a %s.', person.name.first, person.name.last,
  7. person.occupation);
  8. });

⚠️在Mongoose内,所有的callback都使用这个模式callback(error, result)

  • 如果有error存在,则error参数包含a error document。 result的值是null
  • 如果query是成功的,error参数是null, 并且result被填入populated查询的结果。

findOne()的例子:

  1. Adventure.findOne({ type: 'iphone' }, function (err, adventure) {});
  2. // same as above
  3. Adventure.findOne({ type: 'iphone' }).exec(function (err, adventure) {});
  1. // specify options, in this case lean
  2. Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }, callback);
  3.  
  4. // same as above
  5. Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }).exec(callback);
  6.  
  7. // chaining findOne queries (same as above)
  8. Adventure.findOne({ type: 'iphone' }).select('name').lean().exec(callback);

lean选项为true,从queries返回的documents是普通的javaScript 对象,而不是MongooseDocuments。

countDocuments()的例子

在一个collection中,计算符合filter的documents的数量.

query but no callback is passed

一个Query 可以让你使用chaining syntax,而不是specifying a JSON object

例子:

  1. Person.
  2. find({
  3. occupation: /host/,
  4. 'name.last': 'Ghost',
  5. age: { $gt: 17, $lt: 66},
  6. likes: { $in: ['vaporizing', 'talking']}
  7. }).
  8. limit(10).
  9. sort({ occupation: -1 }).
  10. select({name: 1, occupation: 1})
  11. exec(callback)
  12. //等同于使用query builder:
  1. Person.
  2. find({ occupation: /host/ }).
  3. where('name.last').equals('Ghost').
  4. where('age').gt(17).lt(66).
  5. where('likes').in(['vaporizing', 'talking']).
  6. limit(10).
  7. sort('-occupation').
  8. select('name occupation').
  9. exec(callback);

Queries不是promises

可以使用.then函数,   但是调用query的then()能够执行这个query多次。

  1. const q = MyModel.updateMany({}, { isDeleted: true }, function() {
  2. console.log('Update 1');
  3. });
  4.  
  5. q.then(() => console.log('Update 2'));
  6. q.then(() => console.log('Update 3'));

上个例子,执行了3次updateMany()。

  • 第一次使用了callback。
  • 后2次,使用了then()。

注意⚠️不要在query混合使用回调函数和promises。


Virtual Fields (Virtuals)

不存在于数据库,但是像regular field in a mongoose document。就是mock,fake。

Virtual fields的用途:

  • dynamic data
  • creating aggregate fields

例子,一个personSchema,有firstName, lastName2个fields,和一个Virtual fields(fullName),这个Virtual fullName无需真实存在。

另一个例子,兼容以前的database。每次有一个新的schema, 只需增加一个virtual来支持旧的documents。

例如, 我们有上千个用户记录在数据库collection,我们想要开始收集他们的位置。因此有2个方法:

1. 运行一个migration script,为所有old user documents增加一个location field, 值是none。

2. 使用virtual field 并在运行时,apply defaults。

再举一个例,加入有一个大的document,我们需要得到这个document的部分数据,而不是所有的数据,就可以使用virtual field来筛选要显示的数据:

  1. //从Schema中筛选出一些fields,放入虚拟field"info"
  2. userSchema.virtual('info')
  3. .get(function() {
  4. return {
  5. service: this.service,
  6. username: this.username,
  7. date: this.date,
  8. // ...
  9. }
  10. })

定义a virtual :

  1. personSchema.virtual('fullName')创建一个virtual type。
  2. 使用a getter function, get(fn), 返回<VirtualType>this。 (不要使用箭头函数, this是一个instance/document)

完整的例子:

  1. const mongoose = require('mongoose')
  2. mongoose.connect('mongodb://localhost:27017/myproject', {useNewUrlParser: true})
  3.  
  4. var personSchema = new mongoose.Schema({
  5. name: {
  6. first: String,
  7. last: String
  8. }
  9. })

  10. //定义一个virtualType
  11. personSchema.virtual('fullName').get(function () {
  12. return this.name.first + ' ' + this.name.last;
  13. });
  14.  
  15. var Person = mongoose.model('Person', personSchema)
  16.  
  17. // var axl = new Person({
  18. // name: {
  19. // first: 'Axl',
  20. // last: 'Rose'
  21. // }
  22. // }).save((error, result) => {
  23. // if (error) return console.log(error)
  24. // console.log(result)
  25. // })
  26.  
  27. Person.findOne({"name.first": 'Axl'}, (error, result) => {
  28. console.log(result.fullName)
  29. })

上面的例子使用了Schema#virtual()方法。定义了一个虚拟field,并VirtualType#get()方法定义了一个getter。自然也可以定义一个setter,使用set()方法:(关于get,set见

Practical Node.js (2018版) 第7章:Boosting Node.js and Mongoose的更多相关文章

  1. Practical Node.js (2018版) 第5章:数据库 使用MongoDB和Mongoose,或者node.js的native驱动。

    Persistence with MongoDB and Mongoose https://github.com/azat-co/practicalnode/blob/master/chapter5/ ...

  2. Practical Node.js (2018版) 第10章:Getting Node.js Apps Production Ready

    Getting Node.js Apps Production Ready 部署程序需要知道的方面: Environment variables Express.js in production So ...

  3. Practical Node.js (2018版) 第9章: 使用WebSocket建立实时程序,原生的WebSocket使用介绍,Socket.IO的基本使用介绍。

    Real-Time Apps with WebSocket, Socket.IO, and DerbyJS 实时程序的使用变得越来越广泛,如传统的交易,游戏,社交,开发工具DevOps tools, ...

  4. Practical Node.js (2018版) 第3章:测试/Mocha.js, Chai.js, Expect.js

    TDD and BDD for Node.js with Mocha TDD测试驱动开发.自动测试代码. BDD: behavior-driven development行为驱动开发,基于TDD.一种 ...

  5. Practical Node.js (2018版) 第8章:Building Node.js REST API Servers

    Building Node.js REST API Servers with Express.js and Hapi Modern-day web developers use an architec ...

  6. Practical Node.js (2018版) 第4章: 模版引擎

    Template Engines: Pug and Handlebars 一个模版引擎是一个库或框架.它用一些rules/languages来解释data和渲染views. web app中,view ...

  7. Vue.js 学习笔记 第1章 初识Vue.js

    本篇目录: 1.1 Vue.js 是什么 1.2 如何使用Vue.js 本章主要介绍与Vue.js有关的一些概念与技术,并帮助你了解它们背后相关的工作原理. 通过对本章的学习,即使从未接触过Vue.j ...

  8. Node入门教程(7)第五章:node 模块化(下) npm与yarn详解

    Node的包管理器 JavaScript缺少包结构的定义,而CommonJS定义了一系列的规范.而NPM的出现则是为了在CommonJS规范的基础上,实现解决包的安装卸载,依赖管理,版本管理等问题. ...

  9. Node入门教程(6)第五章:node 模块化(上)模块化演进

    node 模块化 JS 诞生的时候,仅仅是为了实现网页表单的本地校验和简单的 dom 操作处理.所以并没有模块化的规范设计. 项目小的时候,我们可以通过命名空间.局部作用域.自执行函数等手段实现变量不 ...

随机推荐

  1. linux下安装tomcat和jdk

    1.现在的linux服务器一般自带jdk,先查询是否已经安装jdk rpm -qa | grep java rpm -qa | grep jdk 如上则是没有安装,可以直接跳到步骤X,安装jdk.否则 ...

  2. Linux 字符设备驱动开发基础(二)—— 编写简单 PWM 设备驱动【转】

    本文转载自:https://blog.csdn.net/zqixiao_09/article/details/50858776 版权声明:本文为博主原创文章,未经博主允许不得转载.    https: ...

  3. 《OFFER14》14_CuttingRope

      // 面试题14:剪绳子 // 题目:给你一根长度为n绳子,请把绳子剪成m段(m.n都是整数,n>1并且m≥1). // 每段的绳子的长度记为k[0].k[1].…….k[m].k[0]*k ...

  4. 【做题】apc001_f-XOR Tree——巧妙转化及dp

    对树上的路径进行操作是十分难处理的事情.一开始的思路主要针对于\(a_i<=15\)这一特殊性质上.于是考虑了\(a_i<=1\)的情况,然而除了糊出一个适用范围极小的结论外,并没有什么用 ...

  5. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)

    还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...

  6. (转)Introduction to Gradient Descent Algorithm (along with variants) in Machine Learning

    Introduction Optimization is always the ultimate goal whether you are dealing with a real life probl ...

  7. Gtk 窗口,控件,设置(添加图片等)

    1.关于窗口   // 创建顶层窗体,后面有POPUP的 GtkWidget *main_window; main_window = gtk_window_new (GTK_WINDOW_TOPLEV ...

  8. FPGA软件使用基础之ISE下载配置 XILINX 下载器使用

    重新编辑 转载于https://www.cnblogs.com/lpp2jwz/p/7306020.html 下载程序 下载BIT 格式程序到FPGA 先插好下载器 在 ISE 中编译完 BIT 文件 ...

  9. HDU 3400 Line belt (三分套三分)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3400 题意: 有两条带子ab和cd,在ab上的速度为p,在cd上的速度为q,在其它地方的速度为r.现 ...

  10. select2 使用方法总结

    官网:http://select2.github.io/ 调用 <link href="~/Content/select2.min.css" rel="styles ...