Home > Net >  Why is my MongoDB estimatedDocumentCount return incorrectly?
Why is my MongoDB estimatedDocumentCount return incorrectly?

Time:10-15

I have a film controller, and inside of it has 2 functions, one to get a movie and one to get a series, both of it has the pagination that I created.

At first, I did the getMovies function, and everything seemed to be fine, working as I expected, but then I copy paste that same function and replaced the query inside of it, the reason I'm separating it into 2 because later on, I want movies and series to act independently (add more filter for both of it in the future).

The problem I'm having is the getSeries, I haven't created any "series" film type, so the count in my getSeries should be 0. But instead, it returns 10. That 10 number is the number I have right now in my MongoDB, but I only create a "movie" film type, not a "series" film type, I haven't created a "series" film type yet up to this point.

Here's my filmController:

const ITEMS_PER_PAGE = 4

const filmController = {
    getMovies: async (req, res) => {
        const page = req.query.page || 1

        const query = { type: "movie" }

        try {
            const skip = (page - 1) * ITEMS_PER_PAGE
            const count = await Films.estimatedDocumentCount(query)
            const films = await Films.find(query).limit(ITEMS_PER_PAGE).skip(skip)
            const pageCount = Math.ceil(count / ITEMS_PER_PAGE)
            res.json({
                films: films,
                pagination: {
                    count,
                    pageCount,
                },
            })
        } catch (err) {
            return res.status(500).json({ msg: err.message })
        }
    },
    getSeries: async (req, res) => {
        const page = req.query.page || 1

        const query = { type: "series" }

        try {
            const skip = (page - 1) * ITEMS_PER_PAGE
            const count = await Films.estimatedDocumentCount(query)
            const films = await Films.find(query).limit(ITEMS_PER_PAGE).skip(skip)
            const pageCount = Math.ceil(count / ITEMS_PER_PAGE)
            res.json({
                films: films,
                pagination: {
                    count,
                    pageCount,
                },
            })
        } catch (err) {
            return res.status(500).json({ msg: err.message })
        }
    },
}

The film model looks like this in MongoDB (I filter it using type: "movie" or "series"):

Film Model

In the POSTMAN, although it doesn't return any items (because I haven't created any series type yet), it still counts that there are 10 items, so it makes the pageCount 3, which is 3 pages.

POSTMAN

Where did I do wrong?

CodePudding user response:

Need to use .countDocuments().

From the docs for the .estimatedDocumentCount() method:

db.collection.estimatedDocumentCount() does not take a query filter and instead uses metadata to return the count for a collection.

So your current code is ignoring the query predicate that you are providing, hence returning the full count of 10 documents in the collection each time.

  • Related