[SERVER-29395] Cannot sort with aggregation by an array entry Created: 29/May/17  Updated: 12/Jul/17  Resolved: 31/May/17

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 3.4.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: L. Page Assignee: Mark Agarunov
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: HTML File reproduce    
Issue Links:
Duplicate
duplicates SERVER-24748 Aggregation does not provide a way to... Backlog
Operating System: ALL
Participants:

 Description   

Cannot sort with aggregation when we have more than one key to sort and one of this are an array entry

No problem if only one key specified:

collection.aggregate([{
    $sort:{
        "events.0.date": -1
        }}])

Problem if more than one key specified and one of this is an array entry:

collection.aggregate([{
    $sort:{
        "events.0.date": -1,
        "_id":1
        }}])

I checked :

collection.aggregate([{
    $sort:{
        "date": -1,
        "_id":1
        }}])

Sorry for my english



 Comments   
Comment by L. Page [ 01/Jun/17 ]

In my exemple to reproduce, if you create an index

{
    "events.0.date" : -1,
    "_id" : 1
}

It's ok, but not:

{
    "events.0.date" : 1,
    "_id" : 1
}

strange, or I did understand nothing to the indexes.

Thanks in any case, I will create a key "update/date" for my code.

Comment by Mark Agarunov [ 31/May/17 ]

Hello pagetronic@gmail.com,

Thank you for providing this information. Looking at the output in the reproduction, I believe this may be caused by the same issue as SERVER-24748, where the projection pipeline treats the 0 in events.0.date as a field name instead of the index key of the events array.

As this behavior looks to be due to the same problem, I've marked this ticket as a duplicate. Please watch SERVER-24748 for updates on the progress of this issue.

Thanks,
Mark

Comment by L. Page [ 31/May/17 ]

I think you can reproduce the bug with the information in the attached file.

(nothing in the log)

Comment by Charlie Swanson [ 31/May/17 ]

I think this might be a duplicate of SERVER-24748?

Comment by Mark Agarunov [ 30/May/17 ]

Hello pagetronic@gmail.com,

Thank you for the report. Looking over the behavior you've described, this may due to a limit of how much memory the $sort stage will use. If this is the case, the allowDiskUse option, as documented in the previos link, may mitigate this issue.

That said, to clarify the issue, could you please provide the complete logs from MongoDB when you're seeing this behavior, as well as the exact error message you are seeing when the query doesn't work.

Thanks,
Mark

Comment by L. Page [ 29/May/17 ]

With a specific index it's ok

{ "events.0.date" : -1, "_id" : 1 }

but not with two

Generated at Thu Feb 08 04:20:45 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.