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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:53:49+00:00 2026-05-22T12:53:49+00:00

I have docs analogous to this: {_id: 1, values : [2,3,4] } {_id: 2,

  • 0

I have docs analogous to this:

{_id: 1, values : [2,3,4] }

{_id: 2, values: [4] }

{_id: 3, values : [3,4,5,6,7,8,9,10,11] }

in which each doc has an array. I need a query that only returns the doc if EACH element of its array match the desired criteria (rather than if ANY element matches).

Eg. something like (but not)

{ ‘values’ : { ‘$gt’ : 1, ‘$lt’: 5} })

which would successfully return the first two but not the third doc, as not all
elements of the third doc’s array ‘values’ match the criteria.

Apparently mongodb uses an implicit OR in queries on arrays, whereas I need AND.

I guess I could manually index each element, eg.:

collection.find({values.0: {$gt:1,$lt:5}, values.1:{$gt:1,$lt:5}, … values.n:{$gt:1,$lt:5}}) but this is a pain with my highly dynamic arrays.

Is there a better way?

Note: I asked this over at mongodb-user but being new to mongodb generated confusion with $all operator. Here I am concerned about the doc array, not a query array. Also, in this numeric case I realize one might write a query that negates the range desired, but in general I won’t be able to write the negation.

  • 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-22T12:53:50+00:00Added an answer on May 22, 2026 at 12:53 pm

    I don’t think there’s any way to do this yet, apart from manually iterating through your documents and checking each value in the array. That’s going to be quite slow because it has to execute JavaScript on each document, and can’t take advantage of any index over col.values.

    Even a $where JavaScript expression query doesn’t seem work here because, possibly because the query contains a callback and is too complex:

    db.col.find("this.values.every(function(v) { return (v > 1 && v < 5) })")
    

    Edit: For some queries, including this one, the JavaScript $where expression needs a return statement, so this works fine:

    db.col.find("return this.values.every(function(v) { return (v > 1 && v < 5) })")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have this code (which is from their docs) $query = DB::query(Database::SELECT, 'SELECT
I noticed the docs have: <%= item_counter %> But I have a partial which
I have a compiled Python library and API docs that I would like to
I have checkedout the docs on this but I am still a bit confused.
I have a docs folder in a Subversion repository named project. I've come to
I have read the GOLD Homepage ( http://www.devincook.com/goldparser/ ) docs, FAQ and Wikipedia to
I have a SVN repository for my project; it keeps code, docs, graphs, etc.
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Any one have any docs or examples of how to add menu items to
Does anyone have any docs/guides/code samples on implementing an SIP based IP or video

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.