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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:32:22+00:00 2026-06-15T10:32:22+00:00

i have deep nested arrays, i am trying to group by some nested array

  • 0

i have deep nested arrays, i am trying to group by some nested array elements and got this to work. however, when i try to use $subtract expression, it fails. any pointers appreciated.

Data:

-scenes: [
  -{
     name: "Greeting_Excited"
     -records: [
          - {
              type: "listeningCycle"
              listeningId: 2
              timestamp: 1354566662041
              -events: [ … ]
              -timeProfile: {
              -timeStampInfo: {
                 earliestStamp: 1354566664530
                 latestStamp: 1354566678412
                }
               -timing: [
                  -{
                      start: 400
                      stop: 556
                      id: "SR-G"
                   }
                 -{
                      start: 559
                      stop: 572
                      id: "NL-G"
                  }
                 ]
                }
               }
             ]
          }
       ]

collection..aggregate( {$unwind:"$scenes"}, {$match: {'scenes.records.timeProfile.timing.id' : 'SR-G'}}, {$group: { _id : {segmentname: "$scenes.name"} , responsetimes : { $push : {$subtract : ["$scenes.records.timeProfile.timing.stop", "$scenes.records.timeProfile.timing.start"]} }}},  {$sort:{responsetimes:1}}   

I am using the mongodb 2.2.1 and native node mongodb driver 1.1.11.

what i am trying to do: 
   -group by scenes [unwind the scenes array], 
   -for a match with SR-G timing-id, 
   -gather all the response times, hence the $subtract (stop-start). 

This is the error msg i see: 

{
"errmsg" : "exception: can't convert from BSON type Array to long",
"code" : 16004,
"ok" : 0
}

it seems that the innermost nested array: $scenes.records.timeProfile.timing is not unwound correctly for $subtract, i tried $project to reduce the fields in the pipeline and played around with various combinations of $project and $group unsuccessfully. also tried to unwind more than once, unsuccessfully.

collection.aggregate( {$project: {segmentname:"$scenes.name", timingid: "$scenes.records.timeProfile.timing.id", timingstart:"$scenes.records.timeProfile.timing.start", timingstop:"$scenes.records.timeProfile.timing.stop"}}, {$unwind:"$scenes"},  {$match: {'scenes.records.timeProfile.timing.id' : 'SR-G'}} )
{ "result" : [ ], "ok" : 1 }

and

collection.aggregate( {$unwind: "$scenes"}, {$project: {segmentname:"$scenes.name",  timingid: "$scenes.records.timeProfile.timing.id", timingstart:"$scenes.records.timeProfile.timing.start", timingstop:"$scenes.records.timeProfile.timing.stop"}}, {$unwind:"$scenes.records.timeProfile.timing"},  {$match: { "scenes.records.timeProfile.timing.id" : "SR-G"}}, {$project: {segmentname:"$scenes.name", timingid: "$scenes.records.timeProfile.timing.id", timingstart:"$scenes.records.timeProfile.timing.start", timingstop:"$scenes.records.timeProfile.timing.stop"}} )
{ "result" : [ ], "ok" : 1 }
  • 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-15T10:32:23+00:00Added an answer on June 15, 2026 at 10:32 am

    Couple problems:

    1. You need to $unwind each nested array level all the way through to timing
    2. $subtract is used with $project, not $group

    Try this:

    collection.aggregate([
        {$unwind: "$scenes"},
        {$unwind: "$scenes.records"},
        {$unwind: "$scenes.records.timeProfile.timing"},
        {$match: {'scenes.records.timeProfile.timing.id' : 'SR-G'}},
        {$project: {
            segmentname: "$scenes.name",
            responseTime: {$subtract: ['$scenes.records.timeProfile.timing.stop', '$scenes.records.timeProfile.timing.start'] }
        }},
        {$group: {
            _id: '$segmentname',
            responseTimes: {$push: '$responseTime'}
        }}
    ], function (err, results) {
        console.log(results);
    });
    

    Outputs:

    [ { _id: 'Greeting_Excited', responseTimes: [ 156 ] } ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested array (only one level deep) like this: $a = array(
I have this nested loop that goes 4 levels deep to find all the
So I have some nested lists (only one level deep) and I'm running into
Basically my array is nested too deep. It has three sub,sub,sub arrays and I
I've got some fairly deep nesting going on in my models where I have
I have a directory with about 100 sub-directories (nested, some 3-4 deep). I need
I have a crash that is happening deep within UIKit for some reason; an
I have a data object that is deep-cloned using a binary serialization. This data
I have a structure that can be very easily represented using a three-deep nested
I have an array of array of hashes which can be nested any level

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.