Delete specified fields from an object by making changes to the entire object

Stored within a mongoDB document is the following data:

{ 
    _id: "123"
    order: 1
    parent: "Dueqmd64nTxM3u9Cm"
    type: "article"
    unit: "kg"
}

Prior to updating the document, all saved data undergoes calculations and validation. Therefore, the complete object is used for updating rather than just data = { order: 2 }:

var data = { 
    order: 2
    parent: "Dueqmd64nTxM3u9Cm"
    type: "article"
    unit: "kg"
}

Collection.update(
    { _id: 123 }, 
    { $set: data }
);

This method works as intended.

However, if attempting to remove certain values from the document, it does not work as expected:

var data = { 
    order: 2
    parent: "Dueqmd64nTxM3u9Cm"
    type: "article"
    unit: undefined
}

Collection.update(
    { _id: 123 }, 
    { $set: data }
);

The expectation was for the unit field to be removed, but this does not occur...

Answer №1

If you want to eliminate certain fields, you can utilize the $unset operator:

information = {
    status: ""
};
Database.update(
    { _id: 123 }, 
    { $set: information }
);

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 jqGrid displays a plus sign even when the subgrid is empty while using XML

I am utilizing jqGrid to display data with a subgrid in XML format. $("#UDFs").jqGrid({ ajaxGridOptions: { contentType: 'application/xml; charset=utf-8' }, datatype: 'xmlstring', datastr: data, xmlReader: { root: "Respo ...

Removing item from Angular service

Within my Angular 2 application, I have created a service called events.service.ts: const EVENTS = { 1512205360: { event: 'foo' }, 1511208360: { event: 'bar' } } @Injectable() export class EventsService { getEvents() ...

What are the best methods for conducting a load test on a JavaScript/AngularJS application?

I'm working on a JavaScript/AngularJS application that communicates with a server using websockets. Is there an efficient method to conduct a load test? I want to simulate what will occur if the app receives 100 calls simultaneously (resulting in 100 ...

Managing a large number of records in a for loop on a Node.js server can be challenging, especially when dealing with nearly

After setting up a NodeJS server and connecting it to a MySQL database with around 5000 users, I needed to read the data from MySQL and update a MongoDB database. I managed to write code for this process. https://gist.github.com/chanakaDe/aa9d6a511070c3c78 ...

I am experiencing issues with icons not loading correctly after changing my module, with error messages indicating issues with cross-origin

Exploring various online tutorials to master the art of Angular programming has been quite an adventure for me. One tutorial introduced a module defined in this manner: .module('MyApp') However, any attempt to modify the name resulted in an er ...

What is the best way to create a search function that can filter objects based on both their years and months of investment?

I recently created a website and I'm struggling with searching through multidimensional arrays or objects (I'm not sure what the difference is). Prior to adding the year and month, I was able to access them directly by using array[id][field]. C ...

Converting an array into a duplicate key object using Node.js - Learn how!

I am looking to transform an array into a duplicate key object in Node.js. Here is the array data I have: { "name" : "ashesh", "address_data" : [ { "city" : "Mumbai", "pincode": "400097" }, { ...

Efficiently adapting html content for Vue localization

I've been utilizing the v-html tag to display HTML in my Vue pages. However, I came across a string that was a complete HTML file with content like this: <html xmlns="https://www.w3.org/1999/xhtml"> <head> .... <style> < ...

Unable to translate data received from API

I have been working on dynamically generating routes in my next.js application. One of the APIs I am using is called getUsers and it returns data in the following format: {"users":[{"_id":"639a87ae8a128118cecae85b","usern ...

Stop the background from being visible using the jQuery Cycle plugin

I'm facing a challenge with the cycle plugin for jquery when creating a slideshow. The transition between slides allows what's underneath to show, but I want a smoother transition where one slide truly fades into the other without the background ...

Is your Scrollmagic failing to function once your website is uploaded to the server?

My website incorporates Scrollmagic to dynamically load sections (changing opacity and adding movement) as users scroll through it. Everything functions perfectly fine when I preview the HTML file on my computer, but once I uploaded it to my hosting serv ...

The AJAX request to the REST service is failing to trigger the success function in the AJAX call

Struggling with some issues related to AJAX, specifically when calling a Java REST server that I've developed. While I am relatively new to AJAX, I have put in quite a bit of effort searching for solutions. The problem arises when making a call from a ...

Extract data from a MongoDB collection at a specific day and time, choosing from a variety of data within the same day but at different times

I need to filter data in a MongoDB collection using Pymongo, based on a specific day and any items prior to that time on that day. Example: A- 2018-01-29 10:01:00 B- 2018-01-29 10:11:00 C- 2018-01-29 10:23:00 D- 2018-01-28 11:01:00 E- 2018-01-28 11:04:0 ...

JavaScript functions stored in electron

I recently completed a fresh electron application. The application is made up of the following files: index.html main.js renderer.js Inside the index.html file, I included a button with an onclick event: <button onclick="myfunction()">Call Functi ...

"Enhancing Collaboration with NextJs Multi-Zones' Unified Header

Currently, I have two applications named admin-shell and delivery-management, both of which are being managed using Multi Zones in NextJs. These applications share a common header with navigation links, but I am encountering difficulties navigating betwee ...

Is there a way to verify the identity of two fields using an external script such as "signup.js"?

My current project involves working with Electron, and it consists of three essential files. The first file is index.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="styles ...

Issue with Cross-Origin Resource Sharing (CORS) encountered while attempting to fetch data from localhost:3000 to 127.0.0.1:

I'm in the process of developing a chatbot that utilizes a Flask backend and a React frontend. The frontend is accessible at http://localhost:3000, while the backend API can be found at . When attempting to send input to the chatbot through a fetch re ...

What could be causing this typescript program to run endlessly without completion?

Here is a login command (Login.ts) I have developed for oclif in typescript. It is designed to gather necessary information in the cli's login command, generate a JWT token, and store it in a file. import Command from '@oclif/command' impor ...

The significance of the '=>' in Lodash syntax

I've encountered an issue while working on an existing project where I'm trying to transfer a portion of it to a different build system using gulp (switching from grunt to gulp). The error seems to be related to the use of '=>' which ...

From HTML to Python to Serial with WebIOPi

I am facing a dilemma and seeking help. Thank you in advance for any guidance! My project involves mounting a raspberry pi 2 b+ on an RC Crawler rover, utilizing WebIOPi for the task. However, I am encountering challenges and unable to find useful resourc ...