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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:57:09+00:00 2026-05-19T00:57:09+00:00

I’m trying to delete the subobject ‘apples’ from my documents and update the ‘fruitInventory’

  • 0

I’m trying to delete the subobject ‘apples’ from my documents and update the ‘fruitInventory’ property, decrease by the amount of apples.

I’m confused on how to proceed, should I use dot notation or do a full text search for apples? I don’t know if this matters but you can assume apples will always be in field 1.

// Document 1 
{
    "1": {
        "apples": 3,
        "fruitInventory": 21,
        "oranges": 12,
        "kiwis": 3,
        "lemons": 3
    },
    "2": {
        "bananas": 4,
        "fruitInventory": 12,
        "oranges": 8,
    },
    "_id": "1"
}

// Document 2
{
    "1": {
        "apples": 5,
        "fruitInventory": 10,
        "oranges": 2,
        "pears": 3
    },
    "2": {
        "bananas": 4,
        "fruitInventory": 6,
        "cherries": 2,
    },
    "_id": "2"
}

Result should be like this:

// Document 1 
{
    "1": {
        "fruitInventory": 18,
        "oranges": 12,
        "kiwis": 3,
        "lemons": "3"
    },
    "2": {
        "bananas": 4,
        "fruitInventory": 12,
        "oranges": 8,
    },
    "_id": "1"
}

// Document 2
{
    "1": {
        "fruitInventory": 5,
        "oranges": "2",
        "pears": "3"
    },
    "2": {
        "bananas": 4,
        "fruitInventory": 6,
        "cherries": 2,
    },
    "_id": "2"
}

Thanks in advance for your help.

  • 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-19T00:57:10+00:00Added an answer on May 19, 2026 at 12:57 am

    In the MongoDB query language there is no way to update a document using information from the document. In other words, there is no way to increment one property with a value from another property. To be specific: there is no way to increment the fruitInventory by X where X is the value of the apples property as an atomic operation.

    The way I would implement this, while avoiding races, is with two findAndModify operations (seen here in Mongo shell syntax):

    var fruit = db.fruit.findAndModify({
     query: {locked: {$ne: true}, "1.apples": {$exists: true}},
     update: {$set: {locked: true}}
    });
    
    var fruitInventory = fruit["1"]["fruitInventory"];
    var apples = fruit["1"]["apples"];
    
    db.fruit.findAndModify({
      query: {"_id": fruit["_id"]},
      update: {
        $set: {"1.fruitInventory": fruitInventory + apples}, 
        $unset: {locked: false, "1.apples": true}
      }
    });
    

    What happens is this: first I find a document which is not locked (more on this later), and has an 1.apples property. The document is returned and updated to get a locked property as a single atomic operation. Since the document now has a locked property it will not be found by the same query, so this query can be run multiple times in parallel without any risk of modifying the same document twice.

    I then extract the 1.fruitInventory and 1.apples, to simplify the next expression.

    The second findAndModify updates the 1.fruitInventory property with the new sum, and unsets the 1.apples property, as well as the locked property (to return the document to its previous, unlocked, state).

    To update all documents you will have to run this code over and over again until the first findAndModify returns null (meaning there are no documents matching the query).

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

Sidebar

Related Questions

No related questions found

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.