Ways to change a value in an array within MongoDb

My array value is being overridden by $set, how can I update it properly?

var obj = // contains some other data to update as well 
obj.images=images;  // updating obj with images []
Units.update({_id: 'id', {$set: obj});

Ultimately, my MongoDB object should look something like this:

{
"_id" : "ZhBgrNFtr2LRPkQrZ",
"sitouts" : "test",
"interiors" : "test",
"features" : "test",
"keyHolder" : "test",
"images" : [
    "1445481257465-8678-20130520_113154.jpg", ## `updated value ` ##
    "1445481257456-3-20130520_113157.jpg",
    "1445481258058-5771-20130520_113202.jpg",
    "1445481258230-9603-20130521_075648.jpg"
]

}

Answer №2

Check out these strategies for enhancing the images array without wiping it out completely:

Revise a Specific Index

Update({_id: id}, {$set: {'images.0': obj.images[0]}})

Add a Single Element

Update({_id: id}, {$push: {images: obj.images[0]}})

Add Multiple Elements

Update({_id: id}, {$push: {images: {$each: obj.images}}})

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 are the steps to validate an Ajax form using Dojo JavaScript?

Currently, I am in the process of validating a form that has been written in Javascript/Dojo before sending it via Ajax. Here is the code snippet: <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js" type="text/javascript" djConf ...

Error message: The attempted import failed because ' is not exported from the module

I am facing an issue with resolving my problem. In a file named lama.d.ts, I have declared a class using the following code: export declare class lama { // code here } After that, I tried to import this class in another file within the same folder ...

js simulate a click on an anchor element based on the child element's id

I am trying to automatically trigger a click on an a tag that contains a div with the id='a'. $(document).ready(function() { $("#chat_list_content a:has(div[id='a'])").click(); //$("#chat_list_content a:has(div[id='a']) ...

How can you identify changes in localstorage and then trigger the activation of a new boot file or reallocate a value using Vue.js or JavaScript?

When users log in, they have the option to choose from 3 domains: dev, demo, and sandbox. The configuration for these domains is set in the Boot.js file using Axios. The boot file is triggered prior to the firing of the Vue instance. Below is the content o ...

Unable to transfer all the formatting from the original file for the window.print() function. Localhost is functioning properly, but encountering issues with production

I'm encountering an issue with getting all styles from my Vue app. I have tried the code provided in this answer: https://stackoverflow.com/questions/52343006/how-to-print-a-part-of-a-vue-component-without-losing-the-style While it works fine on loc ...

Column Arrangements in a Table

Is there a method to insert a new column into a table using the jQuery plugin DataTables? ...

Guide on removing the parent JSON array while retaining the child JSON array

I have been searching for a solution to remove specific JSON array elements, but I have not found one yet. Here is the JSON data I am working with: [ { "KODE": [ { "name": "kode", "value": [ ...

ZeroMQ and Electron integration: Unable to find the specified bindings file

Currently, I am running Windows 7 x64 with nodejs 5.1.0 and Electron 0.35. I carefully followed the instructions provided by the Electron Quick Start app and then proceeded to add the line require("zmq") to the main.js file. However, upon executing npm ins ...

Syntax error occurs while attempting to render the return statement in a React Component

Just starting out with React and encountering a syntax issue that has me stumped. I'm working on a React Component and I thought I had the opening/closing { & }'s correct, but clearly there's something missing based on the error message it&a ...

Guide for implementing onclick event for info boxes in push pins on Bing Maps using ReactJS

I have successfully placed all the coordinates on the map using pushpins, but now I want to create an event that shows an infobox when a pushpin is clicked. Could someone provide an example of how to hide infoboxes and only show them when a pushpin is cli ...

Utilize ViewChild to handle changing elements in Angular 2 and Ionic 2 applications

I am facing an issue where I want to dynamically add multiple IonicSlides, but I am unable to use @viewChild. Can anyone suggest a solution for this problem? Template.html : <div *ngFor="let name of title;let i = index;"> <ion-slide ...

I am interested in excluding the seconds and milliseconds from my date and time

I currently display my time in the following format: 5:34 PM I only want to show the hour and minute. How can I achieve this? ...

Dealing with unresolved promises in async/await functions

What is the best approach for handling unresolved promises? For instance: class Utils { static async thisFunctionOnlyResolvesWhenPassed2AndNeverRejects(number: number) { return new Promise((resolve, reject) => { if(number === 2 ...

Obtaining a Variable Element through Selector

When working with Puppeteer, I am faced with the challenge of clicking on a web button that has a dynamic id like: #product-6852370-Size. Typically, I would use the following code: page.click('#product-6852370-Size'); However, the number withi ...

Access row information within a function while incorporating a data-table component

I am currently facing a challenge with my custom Data-Table being utilized by all developers in the workspace. Due to its complexity, I find it difficult to make changes for minor tasks and have decided to explore alternative solutions. The Data-Table is ...

What is the best way to mix up the images in my memory game?

My JavaScript memory game is functioning properly, but I would like the images to load in a random order. Unfortunately, I'm not sure how to accomplish this. Here is the current setup: var easyImages = ["img/bat.jpg", "img/bug.jpg", "img/cat.jpg", " ...

InnerHTML failing to append HTML content

Almost at the finish line with this code! I already have a footer in place, but when the button is clicked, it smoothly slides down along with its contents. Once it's done sliding, I utilize JavaScript to swap out that footer and its contents with a n ...

Stop the Bootstrap 5 accordion from expanding when the spacebar is pressed in the header text input

Within my React app using react-bootstrap, I have implemented an accordion component that expands or collapses based on the user's interaction with a text input located in the AccordianHeader component. Interestingly, whenever the spacebar is released ...

Can you explain the significance of `Component<Props>` in React Native?

Recently, I started a new react-native project and noticed a change in the component syntax. It now reads export default class WelcomeScreen extends Component<Props>, which is different from what it used to be, export default class WelcomeScreen exte ...

Utilize Play Framework version 2.1 in combination with reactivemongo version 0.8 to tally

I am currently working on a Play 2.1 application that utilizes MongoDB through the Reactivemongo 0.8 plugin. In my project, I have implemented an approach outlined here without using models. One of the methods in my application retrieves all documents fro ...