What is the method for setting a dynamic variable for the monk findoneandupdate property?

I'm struggling to locate and update a field within a document using Monk. The issue lies in my attempt to use a variable in the property section (property: value). The type, as well as the value.

let result = await collection.findOneAndUpdate({type: value}, { $set: { blacklisted: false} })

When I assign the type to something like apple with let type = "apple", it doesn't recognize the type variable in findOneAndUpdate.

However, if I simply input apple directly like this

let result = await collection.findOneAndUpdate({"apple": value}, { $set: { blacklisted: false} })

It works perfectly fine.

I also attempted to turn this into an object, such as: let obj = {type, value} and then used that in the find and update

let result = await collection.findOneAndUpdate(obj, { $set: { blacklisted: false} }) 

Still no success. Any suggestions? Thanks.

Answer №1

If you want to utilize the value of a variable as a key, make sure to employ brackets []

let category = "fruit"
let output = await database.findAndModify(
  { [category]: info },  <----- Implementing [] on category variable 
  { $set: { available: true } }
);

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

The jQuery request for the file was unsuccessful

Currently attempting to use jQuery.get('similar_products_stack.php'); as well as jQuery.get('./similar_products_stack.php'); My similar_products_stack.php file is designed to return an html array, however I keep receiving a 404 erro ...

When deploying my website on Heroku, I utilize mongoose.connect().'

After successfully setting up node.js, npm, mongodb, and mongoose to develop a website with a database locally, I utilized mongoose.connect('mongodb://localhost/test') without any issues. Everything was functioning as expected. However, upon att ...

What is the method to store anchor text in a Session variable?

I'm currently working on a project where I've linked multiple pdf files in the master page. When clicking the anchor, the page redirects to the specified location and displays the pdf in an iframe. Now, I want the text within the anchor tag to be ...

What is the best way to extend a class in NestJS from another class?

I've encountered an issue while attempting to extend a class service from another class service in NestJS and utilize DI to load it in a third service. The error message I'm receiving from Nest is: Error: Nest can't resolve dependencies of ...

`Is it possible to execute page.screenshot within page.evaluate using Puppeteer?`

I am currently working on a web scraping project that involves extracting content from a webpage and capturing a screenshot of it. I have been using Puppeteer for this task, but I have encountered a problem. Whenever I try to call a function that includes ...

Dynamically Adding Filenames in Vue.js with v-for Iteration

I'm currently facing an issue with dynamically adding inputs to my Vue 3 application. Specifically, I have a component that adds both text and file inputs dynamically. While the text input works flawlessly using v-model, the file input requires me to ...

Using Boolean values to filter data in MongoDB with Express.js is a simple yet powerful

I have been working on developing a filter API that is based on the 'Answered' boolean in the schema. However, I am facing some issues with it as the filtering method I tried is not functioning properly. The method I attempted is returning all th ...

The Google Chrome console is failing to display the accurate line numbers for JavaScript errors

Currently, I find myself grappling with debugging in an angular project built with ionic framework. Utilizing ion-router-outlet, I attempt to troubleshoot using the Google Chrome console. Unfortunately, the console is displaying inaccurate line numbers mak ...

How to retrieve the href ID in Node.js using app.js

I am facing an issue with sending parameters to the dashboard while using an href link. I want to include an identifier when clicking on the link that will be passed to the dashboard. The most straightforward solution would involve extracting the id or nam ...

I am having trouble setting an object in the state using React hooks

When assigning an initial value to a state as null, the setState method does not hold the value when assigned. Calling an API in useState returns an object which cannot be directly put into setState. const [userProfile, setProfile] = useState(null); cons ...

Sending an Ajax POST request from a Node.js server

I am running a Node.js server with Socket.IO that communicates with a Python server using Django. I am looking to make a POST request from the Node.js server to the Django server on a specific method without utilizing any jQuery functions due to their depe ...

Fixed columns with horizontal scrolling to prevent Datatables columns from overlapping

I'm facing an issue with my data table created using datatables. I need to fix the first two columns, but when I do so, the other two columns overlap them while scrolling horizontally. If I remove the fixed columns, the scrolling works correctly witho ...

JavaScript: Discovering similar property values within complexly nested arrays and objects

I am facing an issue with matching values from two JSON sources. While using the javascript find method, I have found that it works when the nesting of the "cities" array is one level more shallow (just an array of objects), but doesn't work with deep ...

Identify the absence of search results in an Ajax request to the search page before rendering the HTML content

I am attempting to retrieve JSON code from a page using the following PHP function: private function __ajax_admin_search($username = '') { $result = $this->admin_login->Admin_Username_Ajax($username); $count = count($result); for ...

Is there a way to incorporate a CSS style file switcher into a one-page website design?

After creating a one-page portfolio website with two main stylesheets (default in green and an additional one in red), I am now looking to implement two buttons that allow users to switch between the stylesheets without the need for page reloading. You ca ...

Angular 2 ensures that all of the columns receive updates

Currently, I am facing an issue with my angular 2 editable table created using ngFor. The problem arises when I attempt to edit one column, as all the columns get updated with the same value. One solution could be creating a new component, but I would pr ...

The combination of Netty HTTP and MongoDB results in an unlimited increase in connections

Currently, my backend webservice is powered by Netty and I am utilizing MongoDB as the database. Initially, I was pleased with the exceptional performance achieved through the combination of MongoDB and Netty. However, upon examining the MongoDB logs and r ...

Passing state from React.JS to a link results in an undefined props.location.state

I am facing an issue with passing state to the Link component. When trying to access the state using props.location.satate, it comes up as undefined. Even after attempting to send the state through the to attribute using both pathname and state, it remains ...

How can I receive a response in Express using a promise?

Can I access the response of a post function within an Express code block and send raw HTML? Here's what my current code looks like: let promiseLink = new Promise(function(resolve, reject) { app.post('/recipes', (req, res) => { ...

Is it better to return JSON before streaming a file through an iframe or sending HTTP headers?

I have created a form that utilizes a hidden iframe to submit a file to a script which then modifies the file and returns the altered version. I have realized that I can avoid saving the file anywhere by simply using echo file_get_contents(tmp);, where tmp ...