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

  • Home
  • SEARCH
  • 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 7583663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:44:08+00:00 2026-05-30T18:44:08+00:00

To update elements of a mongo array, I was using syntax like: {$set:{a.0:1238},{a.1:402}} Or,

  • 0

To update elements of a mongo array, I was using syntax like:

{"$set":{"a.0":1238},{"a.1":402}}

Or, more accurately, I was using the C-driver function calls that I think are equivalent to that. This seemed to work fine, but when I look at the object in MongoHub, I see:

a: {"0":1238,"1":402}

instead of:

a: [1238,402]

Does anyone know what is proper syntax to access array elements by index with C-driver? What I am doing now serves my immediate purpose, but I am not sure if there are significant performance differences. Also, I might later need to use operations that require true 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-05-30T18:44:09+00:00Added an answer on May 30, 2026 at 6:44 pm

    If a field didn’t exist, then this dot-notation query will create it as a hash (object) and assign values to keys of that hash. If field exists and is an array, it will behave as you expect. See this session.

    > db.arrays.insert({});
    > db.arrays.find();
    { "_id" : ObjectId("4f518c8b58713e4dbadbfb9f") }
    > db.arrays.update({ "_id" : ObjectId("4f518c8b58713e4dbadbfb9f") }, {$set: {"a.0": 123}});
    > db.arrays.find();
    { "_id" : ObjectId("4f518c8b58713e4dbadbfb9f"), "a" : { "0" : 123 } }
    
    
    > db.arrays.insert({a: []})
    > db.arrays.find();
    { "_id" : ObjectId("4f518c8b58713e4dbadbfb9f"), "a" : { "0" : 123 } }
    { "_id" : ObjectId("4f518cca58713e4dbadbfba0"), "a" : [ ] }
    > db.arrays.update({ "_id" : ObjectId("4f518cca58713e4dbadbfba0") }, {$set: {"a.0": 123}});
    > db.arrays.find();
    { "_id" : ObjectId("4f518c8b58713e4dbadbfb9f"), "a" : { "0" : 123 } }
    { "_id" : ObjectId("4f518cca58713e4dbadbfba0"), "a" : [ 123 ] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Mongo document which holds an array of elements. I'd like to
As per How do I update Array Elements matching criteria in a MongoDB document?
I would like to update multiple elements(with different values) from the same selector. What
i have a query like below and i want to update all elements of
One more question concerning JSF.Particularly, Primefaces. Have following problem with ajax update of elements
I have a Dictionary<int, int> and would like to update certain elements all at
I am trying to update multiple elements at once in a multidimensional array/object and
I have a MySQL query that looks like this: UPDATE `Table` SET `Column` =
I am currently asynchronously fetching json documents to update elements in a few html
Update: giving a much more thorough example. The first two solutions offered were right

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.