Complex query / Index help for MongoDB query
NickName:user18196624 Ask DateTime:2022-12-03T20:48:56

Complex query / Index help for MongoDB query

I have a big mongodb query that has some dynamic properties based on filter options, and including filtering between dates. My query is currently causing scanned Objects / returned results ratio to go above 1000. I am sure my query can be improved as well as adding suitable indexes but I am not sure of the correct index's / improvements to my query.

const userFilter = user ? { assignee: new Types.ObjectId(user) } : null;
    const clientFilter = client ? { client: new Types.ObjectId(client) } : null;

Collection.aggregate([
        {
            $match: {
                ...userFilter,
                ...clientFilter,
                status: 1,
                customer: new Types.ObjectId(customer),
            },
        },
        {
            $match: {
                $or: [
                    {
                        end: {
                            $gte: new Date(end),
                        },
                        start: {
                            $lte: new Date(start),
                        },
                    },
                    {
                        end: {
                            $gte: new Date(end),
                        },
                        start: {
                            $lte: new Date(end),
                            $gte: new Date(start),
                        },
                    },
                    {
                        start: {
                            $lte: new Date(start),
                        },
                        end: {
                            $lte: new Date(end),
                            $gte: new Date(start),
                        },
                    },
                    {
                        start: {
                            $gte: new Date(start),
                        },
                        end: {
                            $lte: new Date(end),
                        },
                    },
                    {
                        start: {
                            $lte: new Date(end),
                        },
                        // @ts-ignore
                        start: {
                            $gte: new Date(start),
                        },
                    },
                    {
                        end: {
                            $lte: new Date(end),
                        },
                        // @ts-ignore
                        end: {
                            $gte: new Date(start),
                        },
                    },
                ],
            },
        },
        {
            $sort: { createdAt: 1 },
        },

}

Depending on filter options we could be filtering by assignee and/or client. We also only want returned results where the start or end date of the document falls within the start and end date filters.

I have tried a few variations of the query itself, as well as adding some compound indexes but had no real success improving the query or index's.

Copyright Notice:Content Author:「user18196624」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74666898/complex-query-index-help-for-mongodb-query

More about “Complex query / Index help for MongoDB query” related questions

Complex query / Index help for MongoDB query

I have a big mongodb query that has some dynamic properties based on filter options, and including filtering between dates. My query is currently causing scanned Objects / returned results ratio to...

Show Detail

force use of index on complex MongoDB query?

i have a large collection of "messages" with 'to', 'from', 'type', and 'visible_to' fields that I want to query against with a fairly complex query that pulls only the messages to/from a particular...

Show Detail

Complex query in mongodb

I want to perform a complex mongodb query from java. I have got foure fields userid, statusid, dateTO, and dateFrom. The complex query is something like (userid Or statusid) And (dateFrom < date...

Show Detail

Optimize MongoDB Query or Index

I am running a quite ordinary MongoDB query, nothing really complex or special and I am wondering if the time it takes (> 1 sec) is normal or if there's something wrong with my indexes. I provided...

Show Detail

Storing a (complex) MongoDB query in MongoDB

I have a MongoDB collection of documents, each document representing a fish. Users of my application can then define a type of fish, by creating a query on the fish collection. This query can be v...

Show Detail

Spring boot MongoDb complex query

I have been learning myself MongoDB implementation in Spring Boot. However, I came into a problem with complex queries. I cannot find any right solution for how to implement complex queries to Mon...

Show Detail

Complex mongodb query with Quarkus

I need to migrate a Spring Boot project to Quarkus. The project has been using Spring Data Mongodb for all the queries. Now I find it difficult to migrate complex queries. One example is public L...

Show Detail

Complex mongodb query with filter group and distinct

I am new to Mongodb and I'm trying to write a complex query. Your help will be really appreciated :-) I have this schemas (After removed most of unnecessray properties) : var Track = Schema({

Show Detail

MongoDB collection index with complex query

I have refined an aggregation query over and over, the query itself is returning exactly the correct information however I am receiving lots of Query targeting alerts as Scanned Objects / Returned...

Show Detail

How does mongodb decide which index to use for a query?

When a certain query is done on a mongodb collection, if there are multiple indexes that can be used to perform the query, how does mongodb choose the index for the query? for an example, in a 'ord...

Show Detail