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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:01:08+00:00 2026-06-14T12:01:08+00:00

Windows 7 64 SP1 MongoDB 2.2.0 Given this data: db.address_book.drop() db.address_book.save({_id:1, addresses: [ {zip:

  • 0

Windows 7 64 SP1
MongoDB 2.2.0

Given this data:

db.address_book.drop()
db.address_book.save({"_id":1, "addresses": [ {"zip": 10018} , {"zip": 10012} ] } )
db.address_book.save({"_id":2, "addresses": [ {"zip": 11216} , {"zip": 10012} ] } )
db.address_book.save({"_id":3, "addresses": [ {"zip": 11215} , {"zip": 10010} ] } )
db.address_book.save({"_id":4, "addresses": [ {"zip": 10011} , {"zip": 10041} ] } )
db.address_book.save({"_id":5, "addresses": [ {"zip": 97202} , {"zip": 97201} ] } )
db.address_book.save({"_id":6, "addresses": [ {"zip": 10038} , {"zip": 97201} ] } )

I want all documents where any of its zip codes are in the range 10012-10040. In other words, I want:

{ "_id" : 1, "addresses" : [ { "zip" : 10018 }, { "zip" : 10012 } ] }
{ "_id" : 2, "addresses" : [ { "zip" : 11216 }, { "zip" : 10012 } ] }
{ "_id" : 6, "addresses" : [ { "zip" : 10038 }, { "zip" : 97201 } ] }

This query returns the desired results:

db.address_book.find( { "addresses": { $elemMatch: { "$and" :[ { "zip": {$gte: 10012} } ,  {"zip": {$lte: 10040} } ] } } } )

Question: Is this the shortest (most elegant) query to get the results?

Each of these queries:

db.address_book.find( { "addresses": { $elemMatch: { "zip": {$gte: 10012} , "zip": {$lte: 10040} } } } )
db.address_book.find( {"addresses.zip" : {$gte:10012, $lte:10040} } )
db.address_book.find( {"$and" :[ { "addresses.zip": {$gte: 10012} } ,  {"addresses.zip": {$lte: 10040} } ] }  )

Return this:

{ "_id" : 1, "addresses" : [ { "zip" : 10018 }, { "zip" : 10012 } ] }
{ "_id" : 2, "addresses" : [ { "zip" : 11216 }, { "zip" : 10012 } ] }
{ "_id" : 3, "addresses" : [ { "zip" : 11215 }, { "zip" : 10010 } ] }
{ "_id" : 4, "addresses" : [ { "zip" : 10011 }, { "zip" : 10041 } ] }
{ "_id" : 6, "addresses" : [ { "zip" : 10038 }, { "zip" : 97201 } ] }

If the first method is the shortest, it makes for a complex C++ query:

mongo::Query selector = QUERY("addresses" << BSON("$elemMatch" <<
   BSON("$and" <<BSON_ARRAY(BSON("zip" << BSON("$gte"<<10012)) <<
                            BSON("zip" << BSON("$lte" << 10040))))));

Am I wrong in thinking that this would be a common query?

  • 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-14T12:01:10+00:00Added an answer on June 14, 2026 at 12:01 pm

    I think the cleanest, most elegant query for this case is a combination of a couple you listed:

     db.address_book.find({addresses: {$elemMatch: {zip: {$gte:10012, $lte:10040}}}})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Windows 7 machine with Visual Studio 2005 SP1 installed. Using this,
Windows 7 64 SP1, Boost 1.42, MS VS 2010 Ultimate, C++ This stripped down
Windows 7 64 SP1 -- MongoDB 2.2.0-rc2 -- Boost 1.42 -- MS VS 2010
Windows 7 64 SP1 -- MongoDB 2.2.0-rc2 -- Boost 1.42 -- MS VS 2010
This is a new install of Visual Studio 2008 SP1 on Windows 2008 (original)
I'm running Visual Studio 2008 sp1 on Windows 7 Build 7100 32bit. I've never
In VB.net 3.5 SP1 I have a Windows Form with three DataGridView controls. One
I have a windows 2003 server (WEb edition) which has .net 3.5 sp1 installed
I'm using Windows Vista Ultimate and today I've installed the Visual Studio 2008 SP1,
I have a new WIndows Server 2008 R2 x64 DataCentre with Framework 3.5 SP1

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.