Search code examples
performancemongoosemongodb-querymongoose-populate

mongoose query low performance when using with populate


I need help with this query. I am querying 8000 documents in AppVisitorwith 4 collections populated.But it is not returning any result. Means it get stuck and after some time nodejs server return 404 error for route in which this query is executed.

 let Keys = yield AppVisitor.find(condition).sort({ createdAt: -1 })
                .populate({ path: 'userDeatils' })
                .populate({ path: 'details' })
                .populate({
                    path: 'skills',
                    match: {
                        $and: [{ value: { $ne: '' } }, { value: { $ne: null } }, { value: { $not: /({R[\d]+})/g } }]
                    }
                })
                .populate({ path: 'courses' });


Solution

  • I guess trying to restructure your collections could help, and use populate function less as its quite a heavy operation.Make sure you know what you are doing when you use populate.