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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:06:24+00:00 2026-05-27T15:06:24+00:00

I am considering using MongoDB to store documents that include a list of key/value

  • 0

I am considering using MongoDB to store documents that include a list of key/value pairs. The safe but ugly and bloated way to store this is as

[ ['k1' : 'v1'] , ['k2' : 'v2'],  ...]

But document elements are inherently ordered within the underlying BSON data structure, so in principle:

{k1 : 'v1', 
 k2 : 'v2',  ...}

should be enough. However I expect most language bindings will interpret these as associative arrays, and thus potentially scramble the ordering. So what I need to know is:

  • Does MongoDB itself promise to preserve item ordering of the second form.
  • Do language bindings have some API which can extract it ordered form — even if the usual “convenient” API returns an associative array.

I am mostly interested in Javascript and PHP here, but I would also like to know about other languages. Any help is appreciated, or just a link to some documentation where I can go RTM.

  • 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-27T15:06:25+00:00Added an answer on May 27, 2026 at 3:06 pm

    From Version 2.6 on, MongoDB preserves the order of fields where possible. However, the _id field always comes first an renaming fields can lead to re-ordering. However, I’d generally try not to rely on details like this. As the original question mentions, there are also additional layers to consider which each must provide some sort of guarantee for the stability of the order…

    Original Answer:

    No, MongoDB does not make guarantees about the ordering of fields:

    “There is no guarantee that the field order will be consistent, or the same, after an update.”

    In particular, in-place updates that change the document size will usually change the ordering of fields. For example, if you $set a field whose old value was of type number and the new value is NumberLong, fields usually get re-ordered.

    However, arrays preserve ordering correctly:

    [ {'key1' : 'value1'}, {'key2' : 'value2'}, ... ]
    

    I don’t see why this is “ugly” and “bloated” at all. Storing a list of complex objects couldn’t be easier. However, abusing objects as lists is definitely ugly: Objects have associative array semantics (i.e. there can only be one field of a given name), while lists/arrays don’t:

    // not ok:
    db.foo2.insert({"foo" : "bar", "foo" : "lala" });
    db.foo2.find();
    { "_id" : ObjectId("4ef09cd9b37bc3cdb0e7fb26"), "foo" : "lala" }
    
    // a list can do that
    db.foo2.insert({ 'array' : [ {'foo' : 'bar'}, { 'foo' : 'lala' } ]});
    db.foo2.find();
    { "_id" : ObjectId("4ef09e01b37bc3cdb0e7fb27"), "array" : 
          [ { "foo" : "bar" }, { "foo" : "lala" } ] }
    

    Keep in mind that MongoDB is an object database, not a key/value store.

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

Sidebar

Related Questions

I'm considering using MongoDb for a backing store for a WPF app that I'm
I'm considering using the Google Contacts API for a webapp, but I'm worried that
I'm considering using MongoDB or CouchDB on a project that needs to maintain historical
I'm considering using mongodb on a new project, but before delving in, I'd like
We're considering using Python (IronPython, but I don't think that's relevant) to provide a
I was recently considering using GoogleData for a hobby project to store my service's
I'm considering using Annotations to define my Hibernate mappings but have run into a
We are considering using PhoneGap for an iPad application that we are developing. We
I'm considering using mysql's built-in aes_encrypt . I normally use blowfish, but mysql doesn't
I'm considering using CouchDB for an upcoming site, but I'm a little confused as

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.