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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:37:16+00:00 2026-05-30T15:37:16+00:00

I need to store some data that is essentially just an array of key-value

  • 0

I need to store some data that is essentially just an array of key-value pairs of date/ints, where the dates will always be unique.

I’d like to be able to store it like an associative array:

array(
    "2012-02-26" => 5,
    "2012-02-27" => 2,
    "2012-02-28" => 17,
    "2012-02-29" => 4
)

but I also need to be able to query the dates (ie. get everything where date > 2012-02-27), and so suspect that I’ll need to use a schema more like:

array(
    array("date"=>"2012-02-26", "value"=>5),
    array("date"=>"2012-02-27", "value"=>2),
    array("date"=>"2012-02-28", "value"=>17),
    array("date"=>"2012-02-29", "value"=>4),
)

Obviously the former is much cleaner and more concise, but will I be able to query it in the way that I am wanting, and if not are there any other schemas that may be more suitable?

  • 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-30T15:37:17+00:00Added an answer on May 30, 2026 at 3:37 pm

    You’ve described two methods, let me break them down.

    Method #1 – Associative Array

    The key tool for querying by “associative array” is the $exists operator. Here are details on the operator.

    So you can definitely run a query like the following:

    db.coll.find( { $exists: { 'field.2012-02-27' } } );
    

    Based on your description you are looking for range queries which does not match up well with the $exists operator. The “associative array” version is also difficult to index.

    Method #2 – Array of objects

    This definitely has better querying functionality:

    db.coll.find( { 'field.date': { $gt: '2012-02-27' } } );
    

    It can also be indexed

    db.coll.ensureIndex( { 'field.date': 1 } );
    

    However, there is a trade-off on updating. If you want to increment the value for a specific date you have to use this unwieldy $ positional operator. This works for an array of objects, but it fails for anything with further nesting.

    Other issues

    One issue with either of these methods is the long-term growth of data. As you expand the object size it will take more space on disk and in memory. If you have an object with two years worth of data that entire array of 700 items will need to be in memory for you to update data for today. This may not be an issue for your specific data, but it should be considered.

    In the same vein, MongoDB queries always return the top-level object. Again, if you have an array of 700 items, you will get all of them for each document that matches. There are ways to filter out the fields that are returned, but they don’t work for “arrays of objects”.

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

Sidebar

Related Questions

I need some kind of priority queue to store pairs <key, value> . Values
I have a situation where I need to store some data that just won't
I need to store some data that looks a little like this: xyz 123
I have some user-specific data that I need to store in SharePoint and make
I need to store some data that follows the simple pattern of mapping an
I need to store some data in a Django model. These data are not
I usually use C++ stdlib map whenever I need to store some data associated
I need to store some sensitive data by encrypting it with atleast 128 bit
I have some things in my app that I need to store, in order
I need to store abitrary binary data (essentially files of various common formats, JPEG,

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.