持续更新 mongoose 细节收集

打个广告 mongoose 中文文档翻译招募中 https://github.com/ssshooter/…

method 和 static 的区别

Schema.prototype.method()
Adds an instance method to documents constructed from Models compiled from this schema.
Schema.prototype.static()
Adds static “class” methods to Models compiled from this schema.
也就是说 method 用于实例(文档), static 用于Model。

内联和populate的区别

Embedding

  • Total document size with embedded data will typically not exceed 16MB of storage (the BSON limit) or otherwise ( as a guideline ) have arrays that contain 500 or more entries.
  • Data that is embedded does generally not require frequent changes. So you could live with “duplication” that comes from the de-normalization not resulting in the need to update those “duplicates” with the same information across many parent documents just to invoke a change.
  • Related data is frequently used in association with the parent. Which means that if your “read/write” cases are pretty much always needing to “read/write” to both parent and child then it makes sense to embed the data for atomic operations.

Referencing

  • The related data is always going to exceed the 16MB BSON limit. You can always consider a hybrid approach of “bucketing”, but the general hard limit of the main document cannot be breached. Common cases are “post” and “comments” where “comment” activity is expected to be very large.
  • Related data needs regular updating. Or essentially the case where you “normalize” because that data is “shared” among many parents and the “related” data is changed frequently enough that it would be impractical to update embedded items in every “parent” where that “child” item occurs. The easier case is to just reference the “child” and make the change once.
  • There is a clear separation of reads and writes. In the case where maybe you are not going to always require that “related” information when reading the “parent” or otherwise to not need to always alter the “parent” when writing to the child, there could be good reason to separate the model as referenced. Additionally if there is a general desire to update many “sub-documents” at once in which where those “sub-documents” are actually references to another collection, then quite often the implementation is more efficient to do when the data is in a separate collection.

mongoDB 原子操作相关

Atomicity and Transactions

一对一,一对多数据的删除

参考

账户锁定机制

Account Locking

find().then() 和 find().exec().then() 的区别

exec()

参数

  • [操作] «String|Function»
  • [回调函数] «Function» optional params depend on the function being called

返回

  • «Promise»

执行一个query

then()

参数

  • [resolve] «Function»
  • [reject] «Function»

返回
«Promise»
执行一个query,返回一个resolved状态带有doc参数,rejected状态带有err参数的Promise。

// 是不是看得很晕,但是源代码居然是?!
Query.prototype.then = function(resolve, reject) {
  return this.exec().then(resolve, reject);
};

就是如果你不需要控制特定操作,直接调用then方法完全就是帮你省略了一步exec。
需要注意的是exec()和find()都可以then,但是find()并不是一个Promise。

mongoose 分页操作

skip + limit 固然可以,但是莓有下面这种方法效率高

condition = {limit:12, type:"next", firstId:"57762a4c875adce3c38c662d", lastId:"57762a4c875adce3c38c6615"};
condition = {limit:12, type:"next", firstId:"57762a4c875adce3c38c6645", lastId:"57762a4c875adce3c38c6675"};
var condition = {};
    var sort = { _id: 1 };
    if (req.body.type == "next") {
        condition._id = { $gt: req.body.lastId };
    } else if (req.body.type == "prev") {
        sort = { _id: -1 };
        condition._id = { $lt: req.body.firstId };
    }

var query = Model.find(condition, {}, { sort: sort }).limit(req.body.limit);

query.exec(function(err, properties) {
        return res.json({ "result": result);
});
    原文作者:ssshooter
    原文地址: https://segmentfault.com/a/1190000014289979
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞