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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:36:47+00:00 2026-06-17T08:36:47+00:00

I have User documents in a collection that have this structure: { _id :

  • 0

I have User documents in a collection that have this structure:

{ "_id" : ObjectId( "4fb54ef46d93b33b21003951" ),
  "activities" : [ 
    { "id" : ObjectId( "4fd66f9001e7fe9f03000065" ),
      "type" : "checkin",
      "date_time_created" : Date( 1339453328000 )}, 
    { "date_time_created" : Date( 1337351732000 ),
      "date_time_updated" : Date( 1337351952635 ),
      "id" : ObjectId( "4fb65e346d93b3fe77000000" )}
  ]
}

I can easily query these documents based on date:

User.where( 
  :activities => { 
    '$elemMatch' => {
      :date_time_created => { '$gte' => start_date, '$lt' => end_date }
    }
  } 
).length

According to logs:

MOPED: 127.0.0.1:27017 COMMAND database=db command={:count=>”users”, :query=>{“activities”=>{“$elemMatch”=>{“date_time_created”=>{“$gte”=>2012-05-10 00:00:00 UTC, “$lt”=>2012-07-12 00:00:00 UTC}}}}} (0.5260ms)

I get the results I need this way.

However, when I’m trying to use the new aggregate function and $match based on the same criteria:

User.collection.aggregate( [
  { "$match" => {
    :activities => {
      '$elemMatch' => {
        :date_time_created => { '$gte' => start_date, '$lt' => end_date }
      }
    }
  } }
]).length

According to logs:

MOPED: 127.0.0.1:27017 COMMAND database=db command={:aggregate=>”users”, :pipeline=>[{“$match”=>{:activities=>{“$elemMatch”=>{“date_time_created”=>{“$gte”=>Thu, 10 May 2012, “$lt”=>Thu, 12 Jul 2012}}}}}]} (0.6049ms)

“start_date” and “end_date” are Ruby Date objects and are essentially the same in both queries. However, when I look at the logs they are changed into different formats. When I try to force the format with something like start_date.strftime(“%Y-%m-%d”) it still doesn’t work.

There are other functions in the aggregate pipeline, but I took them out and I still get the error.

How can I get the aggregate function working to match on Dates?

  • 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-17T08:36:48+00:00Added an answer on June 17, 2026 at 8:36 am

    The following test uses the aggregation framework to approximate the logical equivalent of your query.

    First $unwind the array, $match, then $group using $addToSet in case there are multiple array elements that match. Note that this is a lossy process, but this is acceptable if you only need the count or the ids.

    Hope that this helps.

    test/unit/user_test.rb

    require 'test_helper'
    
    class UserTest < ActiveSupport::TestCase
      def setup
        User.delete_all
      end
    
      test "user aggregate" do
        User.create(
            activities: [
              { id: Moped::BSON::ObjectId("4fd66f9001e7fe9f03000065"),
                type: "checkin",
                date_time_created: Time.at(1339453328000) },
              { date_time_created: Time.at(1337351732000),
                date_time_updated: Time.at(1337351952635),
                id: Moped::BSON::ObjectId("4fb65e346d93b3fe77000000") }
            ]
        )
        start_date = Time.at(1339453328000)
        end_date = start_date + 24*60*60
        assert_equal(1, User.where(
            :activities => {
                '$elemMatch' => {
                    :date_time_created => { '$gte' => start_date, '$lt' => end_date }
                }
            }
        ).length)
        assert_equal(1, User.collection.aggregate( [
            { '$unwind' => '$activities' },
            { '$match' => { '$and' => [
                { 'activities.date_time_created' => { '$gte' => start_date } },
                { 'activities.date_time_created' => { '$lt' => end_date } }
            ] } },
            { '$group' => { '_id' => '$_id', 'activities' => { '$addToSet' =>  '$activities' } } }
        ] ).length)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some embedded documents that look like this: { _id : ObjectId(4e402353bc9f6ec5a6000001), first_name
I have a list box that contains a collection of documents that a user
I have many layers in the Documents that will be displayed on different user
Say I have a collection of users like this:- { _id : 1234, Name
Let's say I have a collection of documents such as: { _id : 0
Let's say you have a collection of documents that are structured as follows: {
id'like to count how many documents a User edited or created. Therefore I have
I have some python code using shutil.copyfile: import os import shutil src='C:\Documents and Settings\user\Desktop\FilesPy'
I have a user document which has a group field. This field is an
I have three models: class User include Mongoid::Document field :name, :type => String has_many

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.