The process of fetching an item from an array in MongoDB

After accepting or canceling a friend request, I am unable to pull it from the array. It seems that when I try to do so, nothing happens.

The query matches for 1 document but no documents are actually modified.

I noticed that removing the requested_at field from the user object resolves this issue.

Where could I be going wrong?

Sample MongoDB Document

{
    "_id" : ObjectId("5cb18680aa024b2d441f93cc"),
    "friends" : [],
    "friend_requests" : [ 
        {
            "user" : {
                "id" : ObjectId("5cb14fd7db537905c89e0a72"),
                "requested_at" : ISODate("2019-04-14T17:51:00.588Z")
            }
        }
    ]
}

Related MongoDB Query

db.getCollection('users').updateOne(
    { _id: ObjectId("5cb18680aa024b2d441f93cc") },
    {
        $pull: {
            friend_requests: {
                user: {
                    id: ObjectId("5cb14fd7db537905c89e0a72")
                }
            }
        }
    });

Outcome

{
    "acknowledged" : true,
    "matchedCount" : 1.0,
    "modifiedCount" : 0.0
}

Answer №1

To establish a condition, utilize dot notation:

db.users.updateOne(
    { _id: ObjectId("5cb18680aa024b2d441f93cc") },
    {
        $pull: {
            "friend_requests": {
                "user.id": ObjectId("5cb14fd7db537905c89e0a72")
            }
        }
    });

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

The image data is not displaying in the $_FILES variable

I am having an issue updating an image in my database. I have a modal that loads using jQuery. When I click on the save modification button, all the form data appears except for the image file, which does not show up in the $_FILES array in PHP. I have tri ...

Generate a dot density map with the help of Google Maps

I am looking to create a dot density map using Google Maps for my state. I have all the counties outlined with their respective populations, and I want to scatter dots randomly within each county to represent the population. The goal is to make a dot densi ...

The plus sign ('+') fails to be matched by the regular expression in Angular 2, although it functions correctly in other testing environments

Check out this code snippet: export const PASSWORD_PATTERN: RegExp = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9`~!@#$%^&*()\-_+={[}\]|\\:;"'<,>.?/]{8,}$/; This constant is utilized in the following manner elsewhere: ...

Node.js assert causes mocha to hang or timeout instead of throwing an error when using assert(false)

I am facing an issue with a mocha test that I have written: describe 'sabah', → beforeEach → @sabahStrategy = _.filter(@strats, { name: 'sabah2' })[0] .strat it 'article list should be populated&ap ...

Vue failing to update when a computed prop changes

As I work with the Vue composition API in one of my components, I encountered an issue where a component doesn't display the correct rendered value when a computed property changes. Strangely, when I directly pass the prop to the component's rend ...

JavaScript code not functioning properly when accessing all information retrieved from Django Model

When I retrieve the endDate field from a Django model using a for loop on an HTML page, I want to verify if all the end dates are earlier than today's date. To achieve this, I am utilizing JavaScript code. Although my code successfully checks the fir ...

Is there a way to initiate a callback function once all the contents of an UpdatePanel have been fully loaded?

Looking for a solution with an ASP.NET UpdatePanel that contains multiple images. I am trying to trigger some javascript code after the UpdatePanel is refreshed, but only after all images have finished loading. I attempted using add_endRequest as a callb ...

What is the best way to ensure that a task is performed only once the DOM has finished loading following an AJAX request

<div id="mydiv"> ... <a id="my-ajax-link"> ... </a> ... ... <select id="my-selectmenu"> ... </select> ... </div> Upon clicking the 'my-ajax-link' link, it triggers an AJ ...

Retrieving information from a tag within an iFrame and passing it to its parent document

I've come across a few example posts on this topic, but for some reason, none of them are working for me. Here is the stack view of what I'm trying to accomplish: <html> <head>...</head> <body> <div>Som ...

Executing PHP to capture and showcase the HTTP request received from one page onto another

As a beginner in web development, I humbly ask for patience as I navigate through unfamiliar territory. If possible, please guide me through the logical steps necessary to accomplish the task at hand. I am currently working with PHP and need assistance i ...

Calculating the percentage difference between two dates to accurately represent timeline chart bar data

I am in the process of creating a unique horizontal timeline chart that visually represents the time span of milestones based on their start and finish dates. Each bar on the timeline corresponds to a milestone, and each rectangle behind the bars signifies ...

Featuring the latest updates on the settings page

I am currently designing a settings page for my planning poker app. I want the additional settings to be visible only if the "become scrum master" checkbox is checked. I have tried using ng-show but it's not working as expected. Here is my code snippe ...

Adjust the Scope in Angular-Charts.js Post-Rendering

I am currently facing a challenge with displaying multiple charts using the angular-charts.js framework. The issue is that I require all the charts to have the same scale, but each chart currently has its own scale based on the displayed data. Unfortunatel ...

Seeking a solution for resolving the problem with HTML1402 when using jquery URL in Internet Explorer?

Why is it that only IE (and not any other browser) gives me the error HTML1402: Character reference is missing an ending semi-colon “;” with this code: <!DOCTYPE HTML> <html> <head> <script src="http://code ...

Get real-time input values

Struggling to retrieve input value in real-time? The goal is to dynamically update the bottom buttons. The first X should match the number of carrots specified in the input above, and the second X will be calculated by a script. The key is to ensure that ...

Switching a set of checkboxes with a main checkbox

I've got this JS code that's doing its job and satisfying all my requirements when one of the checkboxes at the top is checked. However, I'm struggling to uncheck them by following the same process (unchecking the first box is not possible). ...

What is the best way to structure my MongoDB database for optimal organization?

I've recently started delving into the world of database management and have been comparing NoSQL to MySQL. My current project involves creating a web application where users can add events to their calendar. As it stands, I have separate collections ...

The app mounting process encountered an error: the mount target selector "#app" did not return any element

Could you assist me in solving this issue? I've been struggling to find a solution for a while now. Where should I place my <script src="{% static 'js/custom-vue.js' %}"></script> to address the error indicated in the image below ...

Adding flair to a object's value in React JS

In my React JS file, I have a map function that I am using to populate a select dropdown with options. const options = response.map(k => ({ value: k.id, label: k.description ? `${k.name} - ${k.description}` : k.name, })); I ...

Identifying and handling the removal of a complete div element by the user

Is it possible to remove the entire div element if a user tries to inspect the web browser using the script provided below? <script type="text/javascript"> eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/, ...