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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:26:40+00:00 2026-05-27T09:26:40+00:00

I am working with a complex mongodoc that has several embedded documents that represent

  • 0

I am working with a complex mongodoc that has several embedded documents that represent items in an inventory. The whole document can be thought of as a complete inventory. The key ‘sections’ contains another key ‘items’ that contains the individual items and their data such as price, description etc. Each item also has a GUID so it can be looked up individually.

I wrote a stored function to hunt for individual items by id that looks like this:

function (item_id) {
    var pls = db.pricelists.findOne({'sections.items.item_id':item_id});
    if (!pls) {
        return null;
    }
    for (var sect in pls.sections) {
        for (var item in pls.sections[sect].items) {
            if (pls.sections[sect].items[item].item_id == item_id) {
                return pls.sections[sect].items[item];
            }
        }
    }
}

Everything works find so far. The problem is when I want to modify this embedded doc and save it back within its parent document. Examples given from the shell:

var item = db.eval('find_item(1)');
item.users_who_bought = [11,16];
item;

This prints back out to the console the correct item.

{
    "name" : "item00",
    "order" : 0,
    "item_id" : 1,
    "hidden" : false,
    "variants" : [
        {
            "price" : "0.56",
            "label" : "variant000"
        },
        {
            "price" : "1.56",
            "label" : "variant001"
        },
        {
            "price" : "2.56",
            "label" : "variant002"
        }
    ],
    "desc" : "Sociis habitasse, integer pellentesque sit! Nisi purus tincidunt amet mus scelerisque amet, pid enim eros phasellus dolor sociis nunc dictumst sed nunc, integer hac!",
        "users_who_bought" : [11,16]

}

I am at a loss as how to formulate the query to update this embedded item document back into its parent doc. I have tried something like this (the item_id is hardcoded as one here for example):

db.pricelists.update({'sections.item.item_id': 1}, {$set: {'sections.item.item_id[1]': item}})

But this does not work correctly and tries to append to the items array.

Is there a way to wholesale pass in a modified embedded document to an update statement like this, selectively by a value within the document? Or am I thinking about this the wrong way, and I’ll need to write another stored iterator function that will find the item again and then update the entire parent doc with the new values deeply embedded? Thanks!

  • 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-27T09:26:40+00:00Added an answer on May 27, 2026 at 9:26 am

    If you want modify an item inside an array i suggest to use conditional $pull first and $push updated item then.

    In your case it will be a little be complicated still as you have sections array. Thus you may need tou use $ operator on it and then $pull/$push on items array.

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

Sidebar

Related Questions

I'm working on a new project which has some complex javascript. I can't post
We are working on complex solution that contains ASP.Net-based server-side and several client applications
I'm currently working on a project that's getting more complex than I thought it
I'm working on a script that has gotten so complex I want to include
I'm working on Carnatic music scores that involve complex time signatures, that will require
I am working with a complex library that, for historical reasons, is separated into
Debugging a complex jQuery-based application that wasn't working in Opera browser, I traced back
I have a fairly complex multi-threaded Windows service working, but I can't figure out
I am working on two wrapper classes that define real and complex data types.
I'm working on a fairly complex Android application that requires a somewhat large amount

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.