What is the process for updating a document with two specific filter conditions?

I have a structure in my document that appears as follows:

type Document = {
  _id: string
  title: string
  variants: VariantType[]
}

type VariantType = {
  timestamp: Int
  active: Boolean
  content: any[]
}

I am attempting to filter a document using two filter conditions within one query. Initially, I aim to match the _id and then locate a specific variant based on a timestamp.

In my previous query version, I was filtering based on the active key.

const updatedDocument = await allDocuments
        .findOneAndUpdate({ _id: mongoId, 'variants.active': false }, .... };

When I change it to

const updatedDocument = await allDocuments
        .findOneAndUpdate({ _id: mongoId, 'variants.timestamp': timestamp }, .... };

it returns a null result.

Is it possible for MongoDB to match a document in this manner? I have come across the $eq query selector but have struggled to make it work as well.

Answer №1

It turns out that the issue was related to the timestamp that I was sending to the server. I had been retrieving it from the incorrect source, which resulted in it not aligning with any of the available options.

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

I must pause for a specified period before initializing the subsequent component in React Native

Due to restrictions on my API key, I can only make one request every 5 seconds. Therefore, I need to wait for 5 seconds before making another request for NearbyJobs (with the first request being made for PopularJobs). <ScrollView showsVerticalScrollIndi ...

When using ng-click, each function is executed only a single time

After creating a function that combines two separate functions (recorder.start() and recorder.stop()), I encountered a problem where the program could only fire one of the functions at a time when they were added to one ng-click. This meant that in order f ...

Select a hyperlink to access the corresponding tab

I've been playing around with bootstrap and AngularJS. I placed nav-tabs inside a modal-window and have it open when clicking on a link. However, I want the appropriate tab to open directly upon click. For example, if I click on "travel", I want the t ...

ReactJS - What makes ReactJS unique compared to other technologies?

Hey there! I'm currently trying to figure out why this specific code snippet was successful while my previous one wasn't. I've included both snippets below: SUCCESSFUL CODE: handleInputChange = (e) => { let { value } = e.target; ...

A guide on converting character objects to strings

Presented below is an array of characters: Resource {0: "-", 1: "-", 2: "-", 3: "-", 4: "-", 5: "B", 6: "E", 7: "G", 8: "I", 9: "N", 10: " ", 11: "C", 12: "E", 13: "R", 14: "T", 15: "I", .... } I am looking to convert it into the following format: --- ...

Encountering an Empty Array Response in Postman While Utilizing Mongoose with Node.js

I am encountering an issue while trying to post data in my MongoAtlas database using Mongoose on Express and NodeJS. Postman is giving me back an empty array. Can anyone point out what I might be doing wrong? Database: mongodb+srv://username:password@clu ...

Clear a variable that was sent through the post method in an ajax call

Within my JavaScript file, I have the following Ajax code: // Default settings for Ajax requests $.ajaxSetup({ type: 'POST', url: path + '/relay.php' + '?curr=' + currency + "&ver=" + Ma ...

Ensure that all MySQL queries within a for loop have completed before executing the rest of a function in NodeJS

I have been attempting to execute a query in order to retrieve data from one table, then utilize that array of data to fetch information from another table before formatting and returning it as JSON. Despite my efforts, I am struggling to understand how t ...

What is the best way to instruct Maven to package my JavaScript project as a war file from the root directory?

Hey everyone, I'm still searching for a reliable method to package a javascript project. Typically, the default approach is to simply release it to the docroot of your appserver, which I find quite unpleasant. My simple node.js javascript project, w ...

The attempt to cast the value of "X_Value" to an ObjectId in the "X_Model" model at the path "_id" has failed due to being of type string

I'm facing an issue while attempting to update multiple records simultaneously using their IDs. The error message I encounter is puzzling, even ChatGPT couldn't provide a solution. Here's the error: Cast to ObjectId failed for value " ...

Use an external javascript file in AngularJS if permitted

To ensure that my HTML page only loads an external JavaScript file when the variable $scope.jsallowed is set to true, I attempted the following code implementation within my AngularJS based page: <script src="assets/js/slider.min.js" data-ng-if="jsallo ...

Having trouble populating the input text field with the selected value from a dropdown using JavaScript

I have implemented a JavaScript function to retrieve the id of a dropdown select with id='odrid'. The script looks like this: $('#odrid').change(function(){ $.getJSON( 'fetch.php', 'odrid='+$(&ap ...

How do I find out the properties of individual components in Material UI?

Learning material ui has been a challenge for me as I struggle to figure out the properties available for each component. For instance, in a tutorial on YouTube, they used the AppBar component like this: <AppBar title="Enter user details" /> But ho ...

What is the reason behind the jQuery JSON parser requiring double escaping for backslashes?

I'm struggling to comprehend a strange aspect of the JSON data format. Here's the issue: I have a string with a Windows directory path, where backslashes are escaped. However, the jQuery JSON parser seems to require double escaping for some reas ...

I have image paths stored in my mySQL Database, how can I showcase them on my website?

As a beginner, I'm embarking on a learning journey where my aim is to showcase images from my database alongside other content within the same row in my HTML. Below is the JavaScript code snippet from my HTML file: <script type="text/javascri ...

Remove buttons from carousel elements in React Multi-Carousel

Is there a way to hide the arrows in the react-multi-carousel component? https://i.stack.imgur.com/V1nix.png ...

What causes z-index to be ineffective with sticky elements?

In my website, I've implemented rollover effects in a sticky footer and a responsive menu that stays at the top. However, when the menu is opened and extends over the footer, it covers everything except the rollovers. Closed navigation http://www.mus ...

Launching a call to action through Ajax in Zend Framework triggers a redirect, but subsequent calls prove to be effective

I've encountered an intriguing issue that I'm hoping someone else has come across and resolved before. In this particular application, I have implemented a 'change my password' option for users. If a user forgets their password, they c ...

AngularJS: Component fails to load controller

I am managing a directory structure that looks like this --- js/app.js ------- components ----------- header -------------- headerComponent.html -------------- headerComponent.js -------------- headerController.js Within index.html, I have the following ...

Accessing data from Execution Contexts in JavaScript

var value = 10; var outer_funct = function(){ var value = 20; var inner_funct = function(){ var value = 30; console.log(value); // displays 30 console.log(window["outer_funct"]["value"]); // I want to log the value 20 her ...