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 8182903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:59:53+00:00 2026-06-07T00:59:53+00:00

Is there any way to update nested documents by id or some other field?

  • 0

Is there any way to update nested documents by id or some other field?

I use “Full Tree in Single Document” and don’t know beforehand how deep nesting can go. Need to Update, for example, answer with {id:’104′}. I can do that via ‘dot notation’, but since I don’t know the level (depth) of nesting I can’t predict how long my 'comment.answers.answers....answers.' can go.

Is there any way to directly find and update id:’104′, or I still need to pass some kind of depth mark?

{
 title:'some title',
 comment:
     {
        id:'101'
        author:'Joe',
        text:'some comment',
        answers:
        [
            {
              id:'102'
              author:'Joe',
              text:'first answer to comment',
              answers:
                    [
                       {
                           id:'103'
                           author:'Done',
                           text:'first answer to first answer to comment',
                           answers:[]
                       },
                       {
                           id:'104'
                           author:'Bob',
                           text:'Second answer to first answer to comment',
                           answers:[]
                       }

                    ]

            },

            {
            },

            {
            },
        ]
     }
}

I use The Node.JS MongoDB Driver

  • 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-06-07T00:59:54+00:00Added an answer on June 7, 2026 at 12:59 am

    In short there’s no really good way to do a query of this sort. There are a few options:

    You can create a query with a long $or statement, specifying each of the possible nested locations for the document:

    { comment.id: 103, $or [
                            { comment.answers.id: 103 }, 
                            { comment.answers.answers.id: 103 },
                            { comment.answers.answers.answers.id: 103 } 
                           ] 
    }
    

    For more information about the $or operator see the docs.

    In truth, the better and more sustainable solution would be to use a different schema, where all comments and answers are stored in a flat array and then store information about the relationships between the comments in a comments.parent field. For example:

     { comment: [
               { id: 1 }
               { id: 2, parent: 1 }
              ] }
    

    For additional options and a more in depth discussion of possible ways of modeling comment hierarchies, view the Storing Comments Use Case in the MongoDB documentation.

    There are also a number of also strategies in the Trees in MongoDB that you might want to consider.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to perform an update for normalizing a field using a
is there any way how in this situation insert and update DB with single
Is there any way to update my beta testing application on windows phone marketplace?
Is there any way to update a table layout after it has been created?
Is there any way to set/update values in the settings.bundle from within your app.
Is there is any way to update shared OR(Object Repository) in QTP without having
I want to know if there's any way to update from all versions (Example:
Is there any way or possibility to update an apps code and have the
Is there any way to see exact results of an executed update statement in
Is there any way to update the system class loader at runtime? After I've

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.