Delete item from file

In the case that the variable author is defined, an update is made to the mongoDB document to add a new object { assignTo: author }. This will either create or update the existing document.

If author is empty, I need to remove the assignTo field from the document. What is the best way to accomplish this?

if (author) {
    Collection.update(
        { _id: id }, 
        { $set: { assignTo: author } }
    );  
}   
else {
    // remove the object from the collection
}

Answer №1

In order to erase the assignTo field from the document, just use the $unset method:

else {
 Collection.update(
  { _id: id }, 
  { $unset: { assignTo: "" } }
 );  
}

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

Using Laravel to Toggle Element Visibility Based on Role in VueJS

<NavLink v-if="value":href="route('accounts')" :active="route().current('accounts')"> Accounts Here is a scenario I encountered: within my user database, there are designated roles as either User or ...

The response from Ajax is being duplicated with each click

Upon clicking the "Add More" button, I dynamically display two dropdowns. The first dropdown is displayed instantly, while the second dropdown requires an Ajax call to fetch and display its content. The functionality of displaying the second dropdown also ...

How can I use C# with MongoDB to change an integer to a string?

Currently, I am utilizing C# MongoDb.Driver(2.10) to retrieve a document from a collection: MongoClient dbClient = new MongoClient("mongodb://***"); IMongoDatabase db = dbClient.GetDatabase("***"); var collection = db.GetCollection<BsonDocument>(" ...

The failure to build was due to the absence of the export of ParsedQs from express-serve-static-core

Encountered the error message [@types/express]-Type 'P' is not assignable to type 'ParamsArray'. Resolved it by installing specific packages "@types/express": "^4.17.8", "@types/express-serve-static-core": ...

Is there a way for me to retrieve the value nested within an object within another object from this Api response?

Hey there, I'm currently struggling to retrieve the value of faceit_elo from within the csgo object. I attempted using data.games.csgo.faceit_elo but unfortunately it didn't yield any results. Any suggestions on how to access this value would be ...

An unusual 'GET' request has been made to the '/json/version' endpoint in Express.js

Hey there, I'm facing a challenge with my Express project. For some reason, I keep receiving a 404 error due to a mysterious GET request to '/json/version'. The request seems to bypass the defined routers after adding session data and eventu ...

Toggle between different socket.io servers for seamless connectivity

I'm looking for help with a situation where I need a socket.io client to connect to server A, disconnect, and then connect to server B. Any ideas on how I can achieve this? Thanks in advance! UPDATE: Attached below is the code snippet that's gi ...

Tips for incorporating a seamless and interactive parallax effect into your React application

My task involves creating a unique landing page for a website using React. One of the key features I want to include is a parallax effect. The initial layout will have a transparent navbar at the top, along with a header, subtitle, and background image tha ...

Updating values in nested arrays with MongoDB and Mongoose: a guide

My dataset in mongoDB looks like this _id : 5d91caf461f93f13e48ac307, restaurants : [ { name : 'grace restaurant', menus : [ { menu_name : 'chicken soup', price : 100 ...

How is it possible that both of my AngularJS services are being directed to the exact same Restful Java EE web service?

I am facing an issue with my two angularjs services. One is supposed to retrieve a single user while the other should return an array of users. However, both seem to be calling the service that returns an array of users. Can you help me understand why this ...

Using AngularJS to send a $http.post request with Paypal integration

This form utilizes the standard PayPal format for making purchases. <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="<a href= ...

JavaScript must be able to detect when the checkbox value is reversed, which is dependent on the user-entered data

Hey there, I come across a situation where users are selecting a checkbox to insert or update a row of data in a MySQL database through SparkJava/ Java. The functionality is working fine except for a minor glitch. The issue arises when the checkbox behav ...

AJAX seems to be struggling to recognize JSON data as JSON format

I am facing an issue with my AJAX call where the data received from the server is not being treated as JSON, despite setting the datatype to json: function RetrieveMateriasFromServer(callback){ var status_aux; //HTTP request for data from the given UR ...

Preload-webpack-plugin does not support pre-fetching files

I have a query about prefetching and preloading content. In my vue app, I noticed that after building, I have duplicate files loaded in my dist/index.html file. Here is an example: Additionally, the "scripts" are not being preloaded/prefetched as expec ...

What is the quickest and most effective method for toggling more than 10,000 checkboxes using Javascript/jQuery?

I am facing a challenge with a div that contains over 10000 checkboxes. Due to technical limitations, I have no choice but to work with this large number of checkboxes. This is not related to any academic task. <div class="well well-sm" style="min-hei ...

Alert regarding the completion of task in Flask and Celery

I'm currently utilizing celery and flask with a mongodb backend and broker. My main goal is to figure out the most effective way to send a notification after executing a group of synchronous tasks. Both functions are decorated with @celery.task. def ...

Displaying JSON as a table using React JS

I can't seem to display JSON data in a table using the useState hook in React, and I'm not sure why. Here's a snippet from my React file: {` import React, { useState } from 'react' import "../styling/Classes.css"; import data ...

Feeling trapped by the endless stream of AJAX calls

As I was working on building a scheduler with jQuery and AJAX, I encountered a problem with multiple AJAX requests. Although most of the time everything works fine and returns the correct values, occasionally, out of more than 50 requests, I come across so ...

JS backbone require global models in js

I am looking to implement a UserSession model that will handle loading and saving session IDs into cookies using the jQuery cookie plugin. Below is the code for my UserSession model module: define(['jQuery', 'Underscore', 'Backbo ...

JavaScript asynchronous problem

When developing a simple Javascript function to validate user input for City and State, it encounters an issue with asynchronous behavior. The function checks if the user has entered values in the specified input fields and then proceeds to geocode the l ...