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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:00:53+00:00 2026-06-06T06:00:53+00:00

I’m having trouble finding the right syntax to replace an array of objects nested

  • 0

I’m having trouble finding the right syntax to replace an array of objects nested several levels deep within a collection. My preference is to just update the individual properties, but since reading the link below, it seems replacing the whole array is the best bet.

https://jira.mongodb.org/browse/SERVER-831

So I have the following classes as an example:

public class Parent
    {
        public ObjectId _id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public Collection<Child> Children { get; set; }
    }

    public class Child
    {
        public ObjectId _id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public Collection<Pet> Pets { get; set; }
    }

    public class Pet
    {
        public string Name { get; set; }
    }

Using the following code I create a Parent, add some Children, and add a Pet to one of the children.

// Construct Objects
Parent parent = new Parent() { _id = new ObjectId("4f979621682dbc1a8cefecb1") };

Collection<Child> children = new Collection<Child>();
Collection<Pet> pets = new Collection<Pet>();

children.Add(new Child() 
    { _id = new ObjectId("4f979621682dbc1a8cefecaf"), 
      Firstname = "Child", 
      Lastname = "One" });
children.Add(new Child() 
    { _id = new ObjectId("4f979621682dbc1a8cefecb0"), 
      Firstname = "Child", 
      Lastname = "Two" });
pets.Add(new Pet() { Name = "Fishy" });

parent.Children = children;
parent.Children[0].Pets = pets;

// Connect to Mongo
var server = MongoServer.Create("mongodb://localhost/?safe=true");
var db = server.GetDatabase("test");

// Insert into parent collection
MongoCollection<Parent> parents;
parents = db.GetCollection<Parent>("parents");
parents.Insert<Parent>(parent, MongoDB.Driver.SafeMode.True);

This successfully inserts the objects, generating the following JSON result:

{   "_id" : ObjectId("4f979621682dbc1a8cefecb1"),
    "Firstname" : null,
    "Lastname" : null,
    "Children" : 
    [
        {
            "_id" : ObjectId("4f979621682dbc1a8cefecaf"),
            "Firstname" : "Child",
            "Lastname" : "One",
            "Pets" : 
            [
                {
                    "Name" : "Fishy"
                }
            ]
        },
        {
            "_id" : ObjectId("4f979621682dbc1a8cefecb0"),
            "Firstname" : "Child",
            "Lastname" : "Two",
            "Pets" : null
        }
    ]
}

Updating individual document elements also seems to be a trivial process, and works successfully with the following code.

// Change children's name
var query = new QueryDocument { { "Children._id", new ObjectId("4f979621682dbc1a8cefecaf") } };
var update = Update.Set("Children.$.Firstname", "Something");
parents.Update(query, update);

Now the issue I can’t work out is how to replace the Pets array. The following code doesn’t compile as Update.Set doesn’t accept a Collection.

// Change pets information
pets[0].Name = "Fishy2"; // change to pet
pets.Add(new Pet() { Name = "Doggy" }); // add new pet

query = new QueryDocument { { "Children._id", new ObjectId("4f979621682dbc1a8cefecaf") } };
update = Update.Set("Children.$.Pets", pets);
parents.Update(query, update);

So what’s the best process that would enable me to update the details in the Pets array?

  • 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-06T06:00:55+00:00Added an answer on June 6, 2026 at 6:00 am

    Here is the code you are looking for: You’ll need to pass in a BsonArray to the Update.Set value. To create that array, you’ll need to wrap each of the “pets” in a BsonDocumentWrapper so the serialization library knows how to serialize them appropriately.

    var query = new QueryDocument { { "Children._id", new ObjectId("4f979621682dbc1a8cefecaf") } };
    var petDocuments = BsonDocumentWrapper.CreateMultiple(pets);
    var petArray = new BsonArray(petDocuments);
    var update = Update.Set("Children.$.Pets", petArray);
    parents.Update(query, update);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.