简体   繁体   中英

How to get total documents in mongoose?

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

})

How do I get the number of documents (total) when using the limit and skip? It gives me total when I use mongoose-paginate but how can i get total here ?

You can run two separate queries using $facet :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM