Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4038804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:31:06+00:00 2026-05-20T12:31:06+00:00

I have a document structure something along the lines of the following: { "_id"

  • 0

I have a document structure something along the lines of the following:

{
"_id" : "777",
"someKey" : "someValue",
"someArray" : [
    {
        "name" : "name1",
        "someNestedArray" : [
            {
                "name" : "value"
            },
            {
                "name" : "delete me"
            }
        ]
    }
  ]
}

I want to delete the nested array element with the value "delete me".

I know I can find documents which match this description using nested $elemMatch expressions. What is the query syntax for removing the element in question?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:31:06+00:00Added an answer on May 20, 2026 at 12:31 pm

    To delete the item in question you’re actually going to use an update. More specifically you’re going to do an update with the $pull command which will remove the item from the array.

    db.temp.update(
      { _id : "777" },
      {$pull : {"someArray.0.someNestedArray" : {"name":"delete me"}}}
    )
    

    There’s a little bit of “magic” happening here. Using .0 indicates that we know that we are modifying the 0th item of someArray. Using {"name":"delete me"} indicates that we know the exact data that we plan to remove.

    This process works just fine if you load the data into a client and then perform the update. This process works less well if you want to do “generic” queries that perform these operations.

    I think it’s easiest to simply recognize that updating arrays of sub-documents generally requires that you have the original in memory at some point.


    In response to the first comment below, you can probably help your situation by changing the data structure a little

    "someObjects" : {
      "name1":  {
            "someNestedArray" : [
                {
                    "name" : "value"
                },
                {
                    "name" : "delete me"
                }
            ]
        }
    }
    

    Now you can do {$pull : { "someObjects.name1.someNestedArray" : ...

    Here’s the problem with your structure. MongoDB does not have very good support for manipulating “sub-arrays”. Your structure has an array of objects and those objects contain arrays of more objects.

    If you have the following structure, you are going to have a difficult time using things like $pull:

    array [
      { subarray : array [] },
      { subarray : array [] },
    ]
    

    If your structure looks like that and you want to update subarray you have two options:

    1. Change your structure so that you can leverage $pull.
    2. Don’t use $pull. Load the entire object into a client and use findAndModify.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something that looks like the following document structure: public class Document {
I have a document structure like this: { _id: ..., name: ..., keywords: {
I have the following structure in my document: <div id=clientList> <a href=whatever.php att=something>Text</a> <a
I have the following structure in my document: { _id : ObjectId(43jh4j343j4j), array :
i have a structure of document in mongodb as below { _id: some_id, key1:
I have the following structure: class User include Mongoid::Document end class Resource include Mongoid::Document
I have a simple document named Order structure with the fields id, name, userId
I have the following data structure in MongoDB: { _id : ObjectId( xy ),
I have a table with the following basic structure. unique_id | name | original
I have to following XML document structure: <option_set id=1> <option>Yes</option> <option>No</option> <option>Maybe</option> </option_set> <question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.