Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use countdocuments method #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

itamar-owlytics
Copy link

countDocuments method use the pre count hook and then we got the right number of items

For example:
I have 'users' collection in mongoDB and I use the next models:

const userSchema = new Schema({
    name: String,
    is_staff: Boolean
});

const Users = mongoose.model('user', userSchema);

const staffSchema = new Schema({
    email: String
}).add(userSchema);

const Staff = mongoose.model('staff', staffSchema, 'users');

const findQueryTypes = ['count', 'find', ...];

findQueryTypes.forEach(type => {
    staffSchema.pre(type, function (next) {
        const query = { is_staff: true };  
        this.where(query);
    
        next();
    });
});

Both models use the users collection but in the Staff list page I expect to receive only the staff members from the users collection - and it's works - But the count number next to the List label is still same as in the Users page

image

image

@itamar-owlytics
Copy link
Author

itamar-owlytics commented Sep 4, 2022

I know that estimatedDocumentCount method is faster than countDocuments method because the first use the collection metadata but I think that this change is required

https://mongoosejs.com/docs/api/model.html#model_Model-estimatedDocumentCount
https://mongoosejs.com/docs/api/model.html#model_Model-countDocuments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant