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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:11:00+00:00 2026-06-03T17:11:00+00:00

I want to add a new document to the following document having an outer

  • 0

I want to add a new document to the following document having an outer key “User”

{
  name:himani,
  User:[
    {
      _id:e25ffgf627627,
      Name:User1
    },
    {
      _id:fri2i2jhjh9098,
      Name:User2
    }
  ]
};

Below is my code in which I am trying to add a new document to already existing document.
My code is:

var server = MongoServer.Create("mongodb://username:password@localhost:27017/?safe=true");
SafeMode mode = new SafeMode(true);
SafeModeResult result = new SafeModeResult();
var db = server.GetDatabase("himani");
var coll = db.GetCollection("test");
BsonDocument document = new BsonDocument();
document.Add("name", "himani");
result = coll.Insert(document, mode);

BsonDocument nested = new BsonDocument();
nested.Add("1", "heena").Add("2", "divya");
BsonArray a = new BsonArray();
a.Add(2);
a.Add(5);
nested.Add("values", a);
document["3"] = new BsonArray().Add(BsonValue.Create(nested));
coll.Save(document);
var query = Query.And(
    Query.EQ("name", "himani"),
    Query.EQ("3.1", "heena")
);
var match = coll.FindOne(query);

var update = Update.AddToSet("3", new BsonDocument {{ "count", "2" }});
coll.Update(query, update);

I want to add a new document to the User array. I am doing this by above code but its not working.Please tell me the right way of doing it.

  • 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-03T17:11:02+00:00Added an answer on June 3, 2026 at 5:11 pm

    I don’t understand your document structure at all… and the only “user” array I could find in here was a field called “3”. Your code does in fact work and appends a document into the “3” array. The below is the result after running your code. Perhaps you could be more clear as to what you want your document to look like after you have “appended” a user.

    {
      "_id":ObjectId("4fa7d965ce48f3216c52c6c7"),
      "name":"himani",
      "3":[
        {
          "1":"heena",
          "2":"divya",
          "values":[ 2, 5 ]
        },
        {
          "count":"2"
        }
      ]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've following Grid to display data, now when I want to add new record
I want to add new obj of JSON like: 128: { Msg: [{ me:
I want to add a new tab page for every newly opened form. Example:
I want to add a new filter to the Project Explorer, to hide from
I want to add a new feature in a wxPython class. I also want
I want to add a new Stage called field to the default one (i
I want to add a new form to the edit customer page, so far
I have master branch of the project. Then I want to add a new
I want to be able to add new sections (via the 'add' link) and
I want to give users the possibility to dynamically add new columns at the

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.