What is the best way to assign index weight to two id fields in MongoDB?

I need some guidance on creating effective Mongo indexes for a $or query on two ObjectID fields.

Should I prioritize the search to look at the first argument of the $or expression before moving on to the second one, or is it better to split the requests into two and handle the logic in our code?

I attempted to use a compound index with weights but found that it only works for text searches.

Below is my implementation:

@index({ user_id: 1 }, { unique: true, partialFilterExpression: { user_id: { $exists: true } } })
@index({ device_id: 1 }, { partialFilterExpression: { device_id: { $exists: true } } })
@index(
  { user_id: 1, device_id: 1 },
  {
    weights: {
      user_id: 10,
    },
    partialFilterExpression: { user_id: { $exists: true }, device_id: { $exists: true } },
  },
)
@modelOptions({
  schemaOptions: {
    collection: 'test',
    timestamps: {
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    },
  },
})
export class Test extends Base {
  @prop({ required: false })
  public user_id?: ObjectId

  @prop({ required: false })
  public device_id?: ObjectId
}

The query I'm testing:

db.test.find( { $or: [ { user_id: ObjectId('624c6bada5b7f846e80af8cb')}, { device_id: ObjectId('624c6bada5b7f846e80af8ca')}]} )

The results : https://i.sstatic.net/rx3kT.png

The indexes :

https://i.sstatic.net/YAvjv.png

Your insights are greatly appreciated!

Answer №1

Is there a way to ensure that a search prioritizes the first argument in the $or expression before checking the second one?

Unfortunately, MongoDB does not work in a way that allows for this kind of prioritization.

Should we instead split the search into two separate queries and handle the logic in our code?

Yes, if it is essential to enforce this prioritization, splitting the search into two separate queries is necessary.


It might be beneficial to understand how a compound index operates by reading about its prefixes here:

MongoDB can leverage a compound index to support searches on index prefixes.

If your query is not utilizing the compound index, it may be because the query's structure does not align with the index requirements. According to the $or documentation:

Each clause within an $or query can utilize its own index for optimization.

In essence,

{ device_id: ObjectId('624c6bada5b7f846e80af8ca')}
functions as an independent query. Since a compound index necessitates the presence of its prefix within the query, it becomes invalidated in this scenario. The interaction between queries and indexes, particularly regarding Mongo's selection process, requires further exploration for better comprehension.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Migration in Sequelize always adds columns to the public schema

I am currently working on an Express application that uses Sequelize as the ORM and PostgreSQL as the database. The database is set up in a way where each tenant in the application has their own schema. In my migration files, I have included addColumn and ...

Is it possible to optimize the performance of my React and TypeScript project with the help of webpack?

I am working on a massive project that takes 6 to 8 minutes to load when I run npm start. Is there a way to speed up the loading process by first displaying the sign-in page and then loading everything else? ...

Is it possible to invoke a helper function in Node.js from within a callback?

I recently started programming with node.js and I am encountering an error that I can't quite figure out. The function appears to be correctly set up, and I don't believe there are any asynchronous issues because of the self variable I have in pl ...

Passing parameters to res.render in Node.js automatically

Is there a way to streamline the process of excluding or including layout parameters in my routes? With over 50 routes, it becomes tedious to manually add this code snippet to every res.render call. if(req.headers['something']){ res.render(&ap ...

Typescript - Creating a generic type that extends its own type signature

Recently, I stumbled upon the following code snippet: interface Test<T extends Test<T>> { a: number; b: T; } function foo <T extends Test<T>>(el: T): T { ... } I must admit, I am a bit perplexed by this and wondering about ...

Produce configuration files on the fly for Angular Component Testing using @Component declarations

Looking to test an Angular 11 component: @Component({ selector: 'app-foo-page', template: ` <app-header mode='operational' cool='true'></app-header> Some content ` }) export class FooPageComponent { } ...

Changing Minute to HH:MM:SS in Angular 2

Instructions:- Minutes = 1220; Solution:- Time = 20:20:00; Is there a way to convert the minute value into a time format in Angular 2? ...

Repeatedly accessing and updating mongodb can result in server congestion within a nodejs and mongodb environment

When I set up a server using nodejs and mongodb, I noticed that it tends to get stuck when experiencing high concurrency, such as when 10k people are accessing it. This issue seems to be stemming from frequent database reads and writes. 'use strict&ap ...

The 'BrowserRouter' type is lacking the properties specified below... - TypeScript

I'm currently learning TypeScript and attempting to integrate it into my personal project. When I make the call to ReactDOM.render, I encounter this error: No overload matches this call. The last overload resulted in the following error. Argument of ...

Different approaches for filtering values in MongoDB based on another collection

I have 2 collections called Employee and Role. My objective is to extract all the name values from the Employee collection without the title being software engineer in the Role collection. An example of a valid answer would be [Lisa, Nick]. How can I appro ...

Exploring the Functions of app.router in Node.js and Express.js

Before I delve into the topic of app.router, let me first share my understanding of working with middleware. Middleware in Express is used by invoking the function app.use(). When a middleware function is triggered, it has the choice to either pass control ...

Is it possible for webpack to import static files using a dynamic path?

Currently, I am working on a project that involves TypeScript, Webpack, React, and Gatsby. My goal is to import multiple images with various resolutions and webp versions to enhance performance. I have started importing the images in this manner: import ...

Sorting tables in an electron-based application using a Wiki-like interface

In my development efforts for a plugin designed for Obsidian (utilizing their API), I am aiming to implement functionality similar to wiki-style table sorting. This involves having clickable headers that can arrange the table in ascending order with the fi ...

Storing a socket object in a session with Express.js: A guide

io.on('connect', function (socket) { session.userSocket = socket; }); I am attempting to save the socket object in a user's session, however I am encountering difficulties with persistence. The req object in the callback is not reta ...

Connecting MongoDB Compass to an Express.js application does not have to be difficult. Here's

I am currently integrating express.js with Electron and encountering a challenge in connecting MongoDB Compass to ExpressJS. When I launch my express.js application using Electron, I encounter an error related to MongoDB. Despite having set up the database ...

The parameter type 'DateInput' cannot be assigned to the parameter type 'Date'

I am currently utilizing fullcalendar for my project. However, I would like to utilize my local models instead of fullcalendar's model. The issue arises when attempting to create a new instance of my own model, as it displays the following error messa ...

What are the steps to establish a server-sent event with Vue.js?

Could anyone provide guidance on how to implement a server sent event? I have developed a small bidding application where the auctionedItems are displayed in the following component: <div v-for="item in auctionedItems"> <Item :info="item">& ...

Updating from Angular version 12.0.4 to 12.1.0 results in a runtime error. As a temporary solution, we are reverting back to version 12.0

There is a related issue discussed here: Angular: TypeError: Cannot read property 'firstCreatePass' of null However, the problem in that case pertains to different Angular versions and the solution provided did not resolve my issue. The recurring ...

Issue with NestJS verification of AWS Cognito JWT: "Error: applicationRef.isHeadersSent function not recognized"

I have integrated AWS Cognito as the authentication service for my NestJS application. However, when accessing the endpoint without a JWT (unauthenticated), the server crashes and displays the error TypeError: applicationRef.isHeadersSent is not a function ...

The steps for integrating Express with mongoDb

I am currently working on a web project using React, Redux, Express, and Socket.io with the initial setup from ErikRas: https://github.com/erikras/react-redux-universal-hot-example The process seems overwhelming with so many tutorials for React and Redux, ...