簡體   English   中英

如何在貓鼬中獲取總文件?

[英]How to get total documents in mongoose?

usermodel.find({ 'word': { $regex: `.*${word2}.*` } })
        .populate('user').limit(3)
        .skip(page).exec((err, searches) => {

})

使用限制和跳過時如何獲取文檔數(總數)? 當我使用貓鼬分頁時,它給了我總數,但我怎么能在這里得到總數?

您可以使用$facet運行兩個單獨的查詢:

usermodel.aggregate([
    { $match: { your query goes here } },
    {
        $facet: {
            filtered: [ { $limit: 3 } ],
            total: [ { $count: "total" } ]
        }
    },
    {
        $unwind: "$total"
    }
])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM