Working with MongoDB - Updating multiple subarrays within an array

Is it possible to delete elements from multiple subarrays within a single large array? The structure I'm working with is as follows:

{
  "id": {
    "$oid": ""
  },
  "users": [
    {
      "friends": [
        "751573404103999569"
      ]
    },
    {
      "friends": [
        "220799458408005633"
      ]
    }
  ]
}

I have a friend's ID that needs to be removed from all the "friends" arrays within the "users" array.

Answer №1

If you want to utilize the positional operator $[] in this scenario, you can follow these steps:

db.no_more_friends.update({ "users.friends":"the_friend_id"  },{ $pull:{"users.$[].friends":"the_friend_id"}} ,{multi:true})

It's important to note that using {multi:true} will result in removing the user id from all friends sub-arrays across all documents where "the_friend_id" is located.

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

Transfer array data to a React Native component

I wrote a function that fetches data from the backend, maps over it, and adds it to an array called events_data function fetchData() { return fetch('http://**********/users/timetable') .then((response) => response.json()) ...

How can I monitor an input field that already has a value in Vue?

My current setup includes an email input and a watcher that verifies if the input is valid or not. The issue I'm facing is that the watch event only triggers when something new is typed into the email field. This means that if the existing value in th ...

The Express GET route does not support parameters or additional paths

I am facing an issue with making a fetch request when trying to add additional path or parameters... Here is what I want to achieve: const fetchOwnerCardList = () => { fetch("http://localhost:5000/api/card/ownerCards", { method: "GET", header ...

Is PHP OOP all about nurturing objects and functions?

How can I ensure that my array in PHP is properly handled according to the scenario below? How do I prevent overwriting the array and losing the previous values with each 'addArray' call? function createNewArray() { $this->array = new Arr ...

Sending a PHP string formatted as JSON to be processed by jQuery

When attempting to echo a string with a JSON structure, I am encountering issues with the jQuery ajax post not parsing it correctly. My question is whether this string will be recognized as JSON by the jQuery json parse function when echoed in a similar ma ...

PHP arrays retain their reference when passed as arguments

I'm in a bit of a puzzling situation and I'm hoping someone can shed some light on what's going on. I've simplified the scenario to the following case, which can be replicated on PHP Version 5.3.10-1ubuntu3.21: $lines = array("foo" =&g ...

Tips for excluding the Mongodb _id field in clojure?

How can I exclude the id field when retrieving a Document from MongoDB using clojure? For instance, ({:name "maran", :_id #<ObjectId 4e1d4afae8b2ef06ba2b7dd0>} {:name "abimaran", :_id #<ObjectId 4e1d4b12e8b2ef06ba2b7dd1>} {:name "hi", ...

What causes a delay in the start of a child process in Node.js?

I am in the process of developing a global node command line program that can execute any console command I provide it with in a new console window, whether on a Windows or Unix system. The goal is for the program to exit after it spawns its process so tha ...

Is there a way to verify whether a user is currently logged in? (Using everyauth in node.js)

Previously, I relied on client-side auth for my application. Recently, I integrated server-side everyauth and it's functioning well. However, I'm unsure how to perform a function similar to FB.getLoginStatus (which I used in the client-side) when ...

Leverage the power of ssh2-promise in NodeJS to run Linux commands on a remote server

When attempting to run the command yum install <package_name> on a remote Linux server using the ssh2-promise package, I encountered an issue where I couldn't retrieve the response from the command for further processing and validation. I' ...

How can I display the value entered in a text input field when using ajax upload?

Is there a way to display the value of an input type text when using ajax for file upload? I have successfully implemented code to upload files to a directory called attachments_files, but I am facing an issue. How can I retrieve and echo the value from i ...

A versatile jQuery function for extracting values from a :input selector

Is there a universal method in jQuery to retrieve the value of any :input element? I pose this question because I have a webpage containing select and checkbox inputs, as seen in the code below: for (var i = 0; i < arguments.length; i++) { ...

Execute various settimeout or display functions on various elements of a webpage

Just a heads up, I'm not really experienced in development. More on the newbie side of things, but eager to learn. I've been looking for an answer to my question, but so far haven't come across anything that fits my specific situation. It m ...

Is there a way to expand an array formula vertically without requiring the use of Ctrl+Shift+Enter?

I attempted a solution similar to the one discussed in the post titled "excel array formula: not have to 'ctrl-shift-enter'?". Below is the formula I used: =(0*0)+SUM(IF(A10=Bundle!$B$2:$B$58233,1/(COUNTIFS(Bundle!$B$2:$B$58233,A10,Bundle!$N$2:$N ...

Bypass Security Check in Firefox

I am facing issues while trying to automate selenium on a website owned by a third party. When an authentication prompt like this appears in Firefox, Selenium fails: https://i.sstatic.net/VHQB4.png You can see a similar situation when clicking the Displ ...

Navigating React Redux Pages Using React Router

At the moment, I am exploring the possibility of creating an application using React and Redux. Most examples I've come across make use of React Router, so I'm curious about its purpose. My application will consist of multiple pages (at least 20 ...

Choose an image depending on the title, but only if a specific condition is met

var no_images2 = $j("img[title^='Click to enlarge image no_photo']").parent(); $j(no_images2).css('pointer-events','none'); var no_images3 = $j("img[title^='Click to enlarge image no_photo']").parent(); $j(no_images ...

ngTransclude not encompassing any content whatsoever

I am facing an issue with a custom Angular directive that is used in several of my AngularJS views. The directive is set to transclude, but the wrapped content is not appearing. app.directive('card', [function() { return { restrict: "E", ...

What are the distinctions between executing a c++ function and a JavaScript function?

After attempting to execute the c++ program provided below, a compilation error occurred. However, when trying to execute the second javascript code snippet below, no errors were detected. Why is this? ////c++//// #include<iostream> using namespace ...

What could be causing a NullPointerException when attempting to populate an array?

I'm currently working on a task that involves populating an array of BitSets with information from an integer array. //ar is a SIZE*SIZE 1-D int array, SIZE is defined as a constant //declare BitSet array BitSet bs[] = new BitSet[SIZE]; ...