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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:40:45+00:00 2026-05-22T21:40:45+00:00

I am trying to push a List to the subdocument of a document. If

  • 0

I am trying to push a List to the subdocument of a document.
If I insert just a single object as BsonDocument it works as shown below:

BsonDocument subdoc = new BsonDocument {
                 { "_id", ObjectId.GenerateNewId()},
                 {"name", "Mr Bob"}
                };

                var query = Query.EQ("_id", new ObjectId("1234.."));
                var upd = Update.Push("members", subdoc); <-works
                groups.Update(query, upd);

But I need to push a List. I am getting this exception:

"WriteStartArray cannot be called when State is: Initial"

This is the code that is failing:

List<BsonDocument> newMembers = new List<BsonDocument>();
BsonDocument subdoc  = new BsonDocument {
                     { "_id", ObjectId.GenerateNewId()},
                     {"name", "Mr Bob"}
                    };
newMembers.Add(subdoc );
subdoc = new BsonDocument {
                     { "_id", ObjectId.GenerateNewId()},
                     {"name", "Mr Tom"}
                    };                
newMembers.Add(subdoc);

var query = Query.EQ("_id", new ObjectId(id));
var upd = Update.Push("members", newMembers.ToBsonDocument()); <- EXCEPTION
groups.Update(query, upd);

After the insertion, I would see:

groups:
{
  _id:1,
  members:[
   {   
      _id:1,
      name: "Mr Bob"
   },
   {   
      _id:1,
      name: "Mr Tom"
   }
  ]
}
  • 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-22T21:40:46+00:00Added an answer on May 22, 2026 at 9:40 pm

    Since newMembers already List of BsonDocument you no need convert it to BsonDocument again.

    If you want push more than one document to the a nested array you need to use $pushAll:

    var upd = Update.PushAll("members", newMembers.ToArray());
    

    In case if you will need push List items of some class you need convert each item to BsonDocument:

    Lets say that newMembers is List of Member class, then if you need push List to a nested array you should do something like this:

    var upd = Update.PushAll("members", newMembers.Select(x=> x.ToBsonDocument()).ToArray());
    

    Update:

    1. To make subdocument not unique for same user names you can create algoritm that will generate same id from same user names. Most simple algoritm can just replace all spaces with underscore and make ToLower().
    2. Another way load embedd array with $slice and check uniqueness of each subdocument.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to push just the contents of the txt attribute in each question
I'm trying to create an object called List. This object has a method add
In my Script.js I am trying to push the object to array=>array of objects.
Im trying push some data into a CRM system via an XML import. I
I am getting this error when I am trying push my files into heroku
I am trying to push my sidebar to the right of my page. When
I am trying to push a URL with a url encoded hashtag (I'm building
I am trying to push a self-hosted server repository to Git using Git extensions.
I get the following error when trying to push changes to github from the
I have a suspicion why this error happens when I am trying to push

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.