Removing a variable nested key from an object in MongoDB using the $unset command

Here is a list of lesson start times for the instructor named Johnny Appleseed:

{
  _id: 'ka83nala9cya9epsj',
  fullName: Johnny Appleseed,
  schedule: {
   '11/05/2016': '12:30',
   '11/15/2016': '2:30',
   '11/16/2016': '1:30',
   '12/07/2016': '9:30',
   '12/18/2016': '10:30',
   '12/23/2016': '8:30',
  }
  ...
}

I have been attempting to use mongo.update() but haven't found the correct combination. Below is an example that I thought would work, but it's not quite there yet.

function removeStartTime(_instrName, _lessonDate) {
  const _scheduleKey = `schedule.${_lessonDate}`;
  return Instructors.update({ fullName: _instrName }, { $unset: { _scheduleKey: 1 } });
}

The Objective:

We need to unschedule (remove) Johnny Appleseed from his lesson scheduled on 12/18/2016. The updated document should appear as follows:

{
  _id: 'ka83nala9cya9epsj',
  fullName: Johnny Appleseed,
  schedule: {
   '11/05/2016': '12:30',
   '11/15/2016': '2:30',
   '11/16/2016': '1:30',
   '12/07/2016': '9:30',
   '12/23/2016': '8:30',
  }
  ...
}

Your assistance in achieving this is greatly appreciated. Thank you!

Answer №1

To incorporate a variable as a property name, it is necessary to utilize the computed property name syntax which involves enclosing the variable within square brackets:

Employees.update({ empName: _employee }, { $unset: { [_taskKey]: 1 } });

Answer №2

Modifyers.change({name: _modName},{"$remove": {"agenda.12/18/2016": ""} })

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

What is the best way to access the current scope's DOM element within an AngularJS controller?

In my scenario, I have a collection of outerItems each containing a list of innerItems that are dynamically sorted. Whenever the mouse hovers over an innerItem, I need to display a popup window directly above that specific element. To achieve this, I hav ...

Update the state within a forEach iteration

Is there a way to update state when clicking on buttons? I keep getting an error. Error: Uncaught TypeError: this.setState is not a function I understand that this.setState cannot be used here, but I'm unsure of where to bind it. class Popup extend ...

Automating Image Downloads with Puppeteer by Adding Authentication Query String to Image URL

Attempting to save images stored in a web-space account can be challenging. Accessing the private space with credentials and retrieving the image link using Puppeteer works smoothly. However, when the src attribute of the image includes additional authenti ...

Tips for creating a DBRef manually with pymongo

I am attempting to manually create a DBRef in order to include an additional field. However, when I try to input the following: {'$ref': 'projects', '$id': '1029412409721', 'project_name': 'My Project ...

HTML Grid Image Display

I am new to HTML and struggling to create a grid with images where the text appears over the image. I have tried multiple methods but nothing seems to work. I am making progress in my studies, but this time I need some help. Here is the code snippet: &l ...

Choose the final field that is visible when the function is applied

This is the issue I am facing: On our website, we have 5 input fields all sharing the same class name, for example class="inputfield". When the page loads, only the first input field is visible. Next to each field, there is a button that reveals the next ...

Having trouble locating POST variable in PHP after dynamically generating form using Javascript

Within the JavaScript file, I am dynamically creating a form. While I won't show the entire file, it is made up of a series of cards. After that, in the PHP file which is accessed once the submit button is clicked, I am unable to see the POST variable ...

Is it mandatory for the npm install command to only be compatible with Node.js for the script

I've encountered an API that provides a JS SDK, and the instructions on its GitHub page suggest using npm install. My question is whether I need to have Node.js in order to use this SDK since it seems to be designed for it, or if I can simply work wit ...

Dealing with reactive form controls using HTML select elements

I am working with a template that looks like this: <form [formGroup]="form"> <mdl-textfield type="text" #userFirstName name="lastName" label="{{'FIRSTNAME' | translate}}" pattern="[A-Z,a-zéè]*" error-msg ...

A specific image is loading after being clicked in a new page

Despite my efforts to search on various platforms, I have not been able to find a comprehensive solution to my query. Scenario: I currently have a webpage containing a div with multiple images. These images load without any issues and are set to open in ...

Verify if the array contains an unidentified field

I have extracted this dataset from a MongoDB database, and I am looking to retrieve all the objects stored within the "Books" array. Additionally, I would like guidance on how to query for a specific book within this array. { "_id": { "$oid": "5b1 ...

Access the latest data within Sails' Waterline prior to the update process

When using Sails' Waterline, I am tasked with comparing the previous value to the new one and assigning a new attribute based on certain conditions. For instance: beforeUpdate: function(newValues, callback) { if(/* currentValues */.age > newVal ...

Simultaneously filling two dropdown menus with Ajax

Within my HTML code, I have a total of three drop-down lists using the <select><option></option></select> tags. The first drop-down list displays categories, the second one shows subcategories for different products, and the third l ...

What changes should I make to the panUp function in Three.js orbit controls?

Currently, I am tinkering with adjusting the behavior of the panUp function in three.js' orbit controls. By default, this function moves the camera up and down along the y axis. However, I want to change it so that it moves in and out along the z axi ...

"Make updates to the data with the help of a button input

I am working on a form that has two input type buttons. The first button is labeled "edit", and when clicked, it enables the input field. The second button is labeled "save", and when clicked, it disables the input field and should save the new values in a ...

Using AngularJS with ng-view and CSS3 keyframes within a Jade template, but encountering issues with ng-cloak not functioning properly

I am currently enhancing my project based on the code provided by https://github.com/btford/angular-express-blog, which utilizes nodeJS, Express, Jade, and AngularJS. I am using the latest version of AngularJS as of now. Despite successfully implementing ...

Exploring the JSON object tree through recursion using underscore and backbone

If I have a JSON object with nested arrays of unknown depths and I want to pass each array into a _.template function, how can I make that happen? Here's an example of what my JSON object might look like: $start_elements = array ( array( ...

Guide on making a prev/next | first/last button functional in list.js pagination

Is there a way to enhance my paginated index by adding prev/next, first/last buttons? I am curious if anyone has implemented these features using List.js. <script src='//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js&ap ...

Clicking on the ng-repeat will trigger the ng-click event, which populates all the data using ng

I need help including an HTML page using ng-click within ng-repeat. However, it is currently loading all the content for every ng-repeat element. My specific requirement is to only bind(ng-include) the clicked element. Please see the attachment for m ...

PHP script fails to save image file on localhost

Hello everyone, I recently followed the steps provided in the link below to set up PHP on my MacBook Pro running OS 10.12.6. I was able to successfully view phpinfo when I accessed http://localhost/~USERNAME/phpinfo.php, with ~USERNAME being replaced by m ...