【问题标题】:How to use Mongoose skip and limit function in service layer?如何在服务层使用猫鼬跳过和限制功能?
【发布时间】:2019-07-22 08:49:21
【问题描述】:

如何从 DAO 获取文档列表并在服务层执行跳过、限制操作?

这是我的 DAO 函数。

function findAllPosts(first,second) {
    return Post.find({});
}

这是我的服务层。

function findAllPosts(first, second) {
    return new Promises((resolve, reject) => {
        postDao.findAllPosts(Number(first), Number(second)).
            then((data) => {
                var sortingOrd = { 'createdAt': -1 };
            resolve(data.sort(sortingOrd).skip(Number(first)).limit(Number(second)));
            })
            .catch((error) => {
                reject(error);
            });
    });
}

我收到此错误。

TypeError: data.sort(...).skip is not a function

这是模型。

    const mongoose = require('mongoose');

var timestamps = require('mongoose-timestamp');
var mexp = require('mongoose-elasticsearch-xp');
var updateIfCurrentPlugin = require('mongoose-update-if-current').updateIfCurrentPlugin;

var PostSchema = new mongoose.Schema({
    title: String,
    content: String,
    categoryId: String,
    location: String,
    postSummary: String,
    postImage: String,
    userId: String,
    author: String,
    urlToImage: String,
    newsSrc: String
});

PostSchema.plugin(mexp);
PostSchema.plugin(updateIfCurrentPlugin);

PostSchema.plugin(timestamps);
var Post = mongoose.model('Post', PostSchema);

Post
    .esCreateMapping(
        {
            "analysis": {
                "analyzer": {
                    "my_custom_analyzer": {
                        "type": "custom",
                        "tokenizer": "standard",
                        "char_filter": [
                            "html_strip"
                        ],
                        "filter": [
                            "lowercase",
                            "asciifolding"
                        ]
                    }
                }
            }
        }
    )
    .then(function (mapping) {
        // do neat things here
    });

Post.on('es-bulk-sent', function () {
});

Post.on('es-bulk-data', function (doc) {
});

Post.on('es-bulk-error', function (err) {
});

Post
    .esSynchronize()
    .then(function () {
    });

module.exports = Post;

出于特定目的,我从 DAO 层删除了排序、跳过和限制。你能告诉我如何在服务层使用这些吗?是否有将“数据”数组转换为 DocumentQuery 对象的显式方法?

【问题讨论】:

  • 你的猫鼬模型在哪里?
  • 添加了 Post 模型。
  • 我不明白你为什么用data.sort()而不是Post.save()

标签: node.js mongodb mongoose dao service-layer


【解决方案1】:

问题出在 findAllPosts 函数内部。 如果你需要跳过或限制,你应该在你的函数中处理它们。

function findAllPosts(first,second, skip, limit) {
    return Post.find({}).skip(skip).limit(limit);
}

或者完全移除 findAllPosts 函数,直接在主逻辑中使用 Post.find().limit().skip()。

我的建议:实现一个独立的单用途函数来返回您的响应:

function findAllPosts(query, options, cb) {
    Post
    .find(query)
    .select(options.select)
    .skip(options.skip)
    .limit(options.limit)
    .sort(options.sort)
    .lean(options.lean)
    .exec(function(err, docs {
      if(err) return cb(err, null);

      return cb(null, docs);
    });
}

【讨论】:

    猜你喜欢
    • 2013-03-20
    • 2013-10-21
    • 2017-02-04
    • 2020-06-16
    • 1970-01-01
    • 2019-05-31
    • 2015-04-07
    • 1970-01-01
    • 2012-08-08
    相关资源
    最近更新 更多