Enhancing MongoDB Performance by Updating Only the $ref Value in a DBRef Field Type

I am facing a challenge with updating a field in multiple collection documents. The specific field in question is a DBRef, and my goal is to only change the value of the $ref field.

An example of one of the documents is as follows:

{ "_id" : { "$oid" : "50ab682bd3155502a75c7cf6"} , "codeId" : { "$ref" : "version" , "$id" : { "$oid" : "511cb7d5696bdbaf4c85ebb1"}}}

The desired final outcome should look like this:

{ "_id" : { "$oid" : "50ab682bd3155502a75c7cf6"} , "codeId" : { "$ref" : "code" , "$id" : { "$oid" : "511cb7d5696bdbaf4c85ebb1"}}}

I attempted to achieve this using the following approach:

db.collection.update(
   {}, 
   {$set:{"codeId":{$ref:"code"}}},
   false,
   true
);

However, I encountered an issue where the $id was lost and set as null:

{ "_id" : { "$oid" : "50ab682bd3155502a75c7cf6"} , "codeId" : { "$ref" : "code" , "$id" : { "$oid" : null}}}

My main question now is how can I maintain the $id in this update process?

Appreciate any assistance on this matter.

Answer №1

To make updates to a nested document, you can utilize dot notation. It's important to remember that when using dot notation, the path should be enclosed in quotes.

db.collection.update(
   {}, 
   { $set:{
       "codeId.$ref":"code"
     }
   },
   false,
   true
);

For more information on Database References, check out the documentation.

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

React Express Error: Unable to access property 'then' of undefined

I'm facing an issue while trying to server-side render my react app for users who have disabled JavaScript and also for better search engine optimization. However, I am encountering the following error: TypeError: Cannot read property 'then' ...

Navigating JSONP using jQuery

I'm encountering an issue where I can see the correct response in Firebug, but I'm unable to access the data it returns. I need some guidance on how to achieve this. Specifically, I'm attempting to place the timestamp of an entry into a div ...

What is the process for updating the package-lock.json file in Laravel?

GateLab's security feature has identified some known vulnerabilities in the package-lock.json file that need to be updated. The message states: Known security vulnerabilities detected Dependency object-path Version < 0.11.5 Upgrade to ~> 0.11. ...

Transferring multiple data between PHP and JavaScript

Here is the code I am using for the on change event: <script> $('.BIR').change(function() { var id = $(this).val(); //get the current value's option $.ajax({ type:'POST', dataType: "json", ...

Google charts appear only after the second request, not on the initial one

Utilizing Google charts to visually represent company performance data. The Javascript code I have written is as follows: <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> go ...

The infinite scrolling feature encounters issues when scrolling downwards

My infinite scroll code is causing a problem - it only works when scrolling up, not down. Can someone help me fix this issue? <script type="text/javascript"> $(document).ready(function(){ $(window).scroll(function(){ var lastID = $(&apos ...

The error message "Mongoose's static method throwing: TypeError: Model.call has no method 'save'" indicates an issue with the static method in M

Whenever I send a completely valid JSON body through a route in Express, an error related to a static method of a Mongoose model is thrown. Product.save({ ^ TypeError: Object function model(doc, fields, skipId){ bla bla bla ... } has no method &a ...

How can I quickly duplicate the design of a JSON Object?

Perhaps the title is a bit basic. Essentially, I am looking for something similar to mysqldump ... --no-data .... For instance, I have a JSON object structured like this: { "key1" : "value1", "key2" : "value2", "key3" : { "key3a" : 1, "key ...

Can you provide the date time format used in the JSTL fmt tag?

To display date and time in JSTL, the fmt tag can be utilized. Details can be found here In order to format the date for use with front end tools like the datatable, a specific date format needs to be specified. By using parameters such as type or dateSty ...

Switching the displayed image depending on the JSON data received

As a beginner in javascript and jQuery, I am working on displaying JSON results in the browser. My goal is to generate dynamic HTML by incorporating the JSON data. Below is an example of the JSON structure: [{"JobName":"JobDoSomething","JobStatus":2,"JobS ...

Utilize JavaScript to randomly choose images as background tiles in HTML

Currently, I am in the process of developing a game using HTML/CSS/JavaScript. My background is currently set to a single image (100px / 100px) being repeated vertically and horizontally to tile across the entire page body; CSS: body { background-ima ...

Master the art of manipulating tags in Django templates using JavaScript

In my Django quiz app, there are two main components. The first part involves displaying 10 sentences with corresponding audio to help with memorization, one per page. The second part consists of asking questions based on the same set of sentences. I initi ...

Exploring the wonders of ReactJs in the ComponentDidMount

I am encountering some issues with my app. Although I am not a Javascript expert, it seems like an easy fix to me. I need to make the following call: this.props.onNavStyleChange(NAV_STYLE_FIXED); to change the navigation when this page loads. However, I ...

Combining conditions and unwinding in MongoDB aggregation for a more efficient and concise query

Can someone help me understand the inner workings of this code snippet: I have a collection containing data structured like this: { _id: 5a854ee830093123a44bb7ec, item: 124103, owner: 'ZZZ', quantity: 50, //ranging from 1 to X (X ...

Using JavaScript to save a file with a data URL in Internet Explorer

Need help with file conversion on different browsers. I developed an app that converts files, and everything was working perfectly in Chrome. However, when it comes to IE (10/11/Edge), I'm facing some challenges: 1) The HTML5 download attribute does ...

Setting input field value using jQuery depending on the attribute value

Here is an example of an input field: <input data-checkout="card-number" type="tel" placeholder="card number" autocomplete="off" class="input-control" value=""> I am looking to use jQuery to set a value in this field. How can I retrieve the attribu ...

Retaining selected items in Jquery UI Multiselect widget even after postback

I want to implement a GridView in asp.net that allows filtering on each column of the gridview, similar to Excel. To achieve this, I created a GridView and utilized the Jquery multiselect widget on the header of each column for filtering. <cc1:GridView ...

Customize Nivo LightBox to apply to only one video separately on the page

I've recently implemented the Nivo LightBox script on my website for a video popup effect. However, as a newcomer to jQuery, I'm struggling to customize the lightbox to only trigger when clicking a specific link for my video, rather than every li ...

Tally the values entered into the text input field

I am interested in counting the number of IDs within an input of type "text" The values return like this: 1, 4, 6, etc. <input type="hidden" class="selected_ids" value="selected_ids" name="selected_ids[]" multiple="yes" id="selected_ids" /> ...

Error encountered while invoking web server method in C# through ajax resulting in a 500 Internal Server Error

Occasionally encountering a 500 internal server error when calling a server method from an AJAX request has left me perplexed. The inconsistency of the issue, sometimes working fine and sometimes not, is baffling. To add to the confusion, no changes were m ...