Making a modification to a specific field within a MongoDB collection

I need to make a change to the editing field:

{
    "_id" : "C7PgEtzToNwHgJb6e",
    "metadata" : {
        "type" : "anything",
        "editing" : false
    }
}

If I execute the following code...

Collection.update(
    { _id: id },
    { $set: { metadata: { editing: true } } }
);

...the type field will be deleted:

{
    "_id" : "C7PgEtzToNwHgJb6e",
    "metadata" : {
        "editing" : true
    }
}

Answer №1

By utilizing the dot notation, you can guarantee that only the specified field value will be modified rather than the entire field:

Collection.update(
    { _id: id },
   { $set: { "data.editing": 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

Creating an Array using the values of a JSON object in jQuery

There is a basic JSON file (test.json): {"personnes":[ { "name":"Super", "firstname":"Mario", "adresse":["45 rue du poirier","6700","Strasbourg"], "departement": "bas-rhin", ...

Learn how to choose multiple rows in a table using a combination of ctrl+click and Shift+click commands within react js

I'm struggling with a problem and need some assistance. I'm trying to implement a feature where a user can highlight a table row by pressing ctrl + left click, and when they press shift + left click, it should highlight all the row data from one ...

Continue to receive fresh activities for finished requests in rsocket

Given a scenario where an Rsocket endpoint is set up using Spring, @MessageMapping("chat.{chatId}") Flux<Message> getChats(@DestinationVariable String chatId) { Mono<Chat> data = chatRepository.findById(chatId); ...

Error: The function app.all is not defined

This is a basic Angular page that I have set up. <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> ...

Instead of emitting child data, a PointerEvent object is being returned

I am currently developing a Vue component library and one of the components I have built is a button that contains data for its width and left position. My goal is to emit this data to the parent (a tabs component) when the button is clicked. After trouble ...

What is the best way to eliminate the background color from one span after selecting a different one?

I created a span element that changes its background color when clicked, but I want the background color to switch to the newly clicked span while removing it from the previously clicked one. Can someone help me achieve this? CSS list-sty ...

Guide to activating "Snapping" feature using the "Select" function in Openlayers 3

I have created an application using OpenLayers 3 that allows users to draw lines or points on a map and add tags to them. While the existing functions in OL3 are helpful for drawing and modifying features, I found it challenging to select the items I drew ...

The HTML textarea is not updating properly when using jQuery's keypress event

I am facing an issue with my forms on a webpage, each containing an html textarea: <textarea name="Comment" class="inputTextArea">Hello World!</textarea> There is a javascript event handler that automatically submits the fo ...

Tips for creating a script to calculate the population count of a 16-bit integer with either php or javascript

Looking to write a function that calculates the population count of a 16-bit integer in either php or javascript? Population count refers to the number of "turned on" bits, essentially counting the number of ones in the binary representation. For example, ...

Turn off Drag and Drop Feature for jQuery File Upload

Currently, I have implemented an ajax file uploader on my website using the following plugin: https://github.com/blueimp/jQuery-File-Upload On one of my pages, there are two controls for uploading files - one for images and another for documents. Both con ...

Determining the spatial location of an object in Three.js in relation to the camera for the purpose of creating a trail

In my scene, I have an object (a pen) that rotates around its axis in the render loop using the following code: groupPen.rotation.y += speed; groupPen.rotation.x += speed; Additionally, I have a TrackballControls that allows the user to rotate the entire ...

tips for integrating html5 elements with django forms

I am interested in utilizing the following code: # extra.py in yourproject/app/ from django.db.models import FileField from django.forms import forms from django.template.defaultfilters import filesizeformat from django.utils.translation import ugettext_ ...

How to send an html form with php, store it in a MySQL Database, and utilize Ajax and jQuery for

As a beginner in PHP form creation, I have been exploring various tutorials and combining different techniques to create my form. However, I am facing challenges as none of the tutorials cover everything comprehensively from beginning to end. While I beli ...

Encountered an eas error during Android build process

When I try to execute the command "eas build --platform android" I encounter the following error message: "✖ Build failed ...

Customizing split applications on Windows 8

Windows 8 split app displays tiles with images, and I want each tile to show a different image. Below is the code from the default.html page: <div class="item"> <img alt="some text" data-win-bind="source: imagePath"> <div class="item-overl ...

Control the line height in DataTables

Is there a way to adjust the line height for a tr using either DataTables settings or CSS? I've attempted different methods, but nothing seems to change the line-height. https://i.sstatic.net/GwFaD.png Table CSS ...

Assistance with Blackberry Webworks (Widget) JavaScript & XML/JSON Integration

Let me paint you a picture... I am in the process of crafting a Blackberry WebWorks (widget) that serves as a gateway to an external website. The majority of the pages reside on this remote site, with the exception of a user customizable homepage where us ...

Update the page content once the popover is closed. Working with IONIC 3

In my application, there are 4 tabs, each displaying different data based on a specific configuration. The header of the page includes a popover component with settings options. When a user adjusts the settings and returns to a tab page, the content on th ...

The reactivity of a Vue.js computed property diminishes when it is transmitted through an event handler

Within my main application, I've implemented a Modal component that receives content via an event whenever a modal needs to be displayed. The Modal content consists of a list with an associated action for each item, such as "select" or "remove": Vue. ...

Is there a harmonious relationship between next.js and mongodb?

I searched extensively for a solution to my issue but still haven't found a clear answer. When working with MongoDB, it's common practice to establish a connection and then close it after completing the task. However, in environments like next.js ...