4

I am trying to get array count among all documents that array content dates that less and greater than particular input given by me. I edited following query many ways but none of those won't get the result I expected.

db.user_log.aggregate(
      {$match: {"user_id" : "2"}}, 
      {$unwind: "$meta_data.access_times"},
      {$group: {_id: "$user_id", number: {$sum: 1 }}}
    ) 

above query give me sum of array count given user_id among all the documents. Result as follows.

{
    "_id" : "2",
    "number" : 26.0
}

How do I use gt and lt in above query?

my array -

"access_times" : [ 
            ISODate("2017-02-25T07:02:31.935Z"), 
            ISODate("2017-03-25T07:02:39.817Z")
        ],

I am going to made this array for mongo terminal and java

2
  • Can you add sample document and expected json output ? Commented Mar 25, 2017 at 6:55
  • every document has a "meta_data.access_times" array. I need count of array elements that between two dates Commented Mar 25, 2017 at 6:56

1 Answer 1

3

Mongo terminal query

db.user_log.aggregate(
  {$unwind: '$meta_data.access_times'},
  {$match: {"user_id" : "2", "meta_data.access_times" : { $gt : ISODate("2017-03-23T00:00:00.000Z"), $lt : ISODate("2017-03-24T00:00:00.000Z")}}},
  {$group: {_id: null, 'sum': { $sum: 1}}},
  {$group: {_id: '$_id', total_sum: {'$sum': '$sum'}}}
) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.