[SERVER-21042] Sorting data within arrays Created: 21/Oct/15  Updated: 22/Oct/15  Resolved: 22/Oct/15

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Boris Assignee: Stennie Steneker (Inactive)
Resolution: Done Votes: 0
Labels: mongo
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

CentOS 7


Attachments: HTML File mongodb_issue    
Operating System: ALL
Participants:

 Description   

I have very simple collection with data in array. I have explained the case in attached file.



 Comments   
Comment by Stennie Steneker (Inactive) [ 22/Oct/15 ]

Hi Boris,

The sort operation in a find() query applies at the document level and does not reorder fields within a document.

In order to sort values within an array and return your desired result, you need to use the aggregation framework:

db.compact_channel_data.aggregate(
 
    // $unwind outputs a document for each element in the array
    { $unwind: "$data" },
 
    // Limit output fields to sensor_datetime
    { $project: {
        _id: 0,
        sensor_datetime: "$data.sensor_datetime"
    }},
 
    // Sort by sensor_datetime
    { $sort: { "sensor_datetime": 1 } }
)

Example output in MongoDB 3.0.7:

{
  "result": [
    {
      "sensor_datetime": 1445429500.624
    },
    {
      "sensor_datetime": 1445429500.625
    },
    {
      "sensor_datetime": 1445429500.659
    },
    {
      "sensor_datetime": 1445429500.66
    },
    {
      "sensor_datetime": 1445429500.671
    },
    {
      "sensor_datetime": 1445429500.679
    }
  ],
  "ok": 1
}

Please note that the SERVER project is for reporting bugs or feature suggestions for the MongoDB server.

For MongoDB-related community support discussion please post on the mongodb-user group (http://groups.google.com/group/mongodb-user) or Stack Overflow. See our Technical Support page for additional support resources.

Regards,
Stephen

Comment by J Rassi [ 21/Oct/15 ]

Pasting contents of attachment "mongodb_issue":

Data in collection compact_channel_data record:
{
    "_id" : ObjectId("562786dc99d0948a7f1fb3de"),
    "channel_name" : "NPr-1.1",
    "valid" : true,
    "data" : [
        {
            "ieq_input_raw_value" : "010010000000000000000000000000000000000000000000",
            "sensor_datetime" : 1445429500.625,
            "db_datetime" : ISODate("2015-10-21T14:11:40.625Z")
        },
        {
            "ieq_input_raw_value" : "110010000000001000000000000000000000000000000000",
            "sensor_datetime" : 1445429500.66,
            "db_datetime" : ISODate("2015-10-21T14:11:40.660Z")
        },
        {
            "ieq_input_raw_value" : "110010000000001001001000000000000000000000000000",
            "sensor_datetime" : 1445429500.624,
            "db_datetime" : ISODate("2015-10-21T14:11:40.624Z")
        },
        {
            "ieq_input_raw_value" : "110010000000001011001000000000100000000000000000",
            "sensor_datetime" : 1445429500.659,
            "db_datetime" : ISODate("2015-10-21T14:11:40.659Z")
        },
        {
            "ieq_input_raw_value" : "110010000000001010001000000000100000000000000000",
            "sensor_datetime" : 1445429500.671,
            "db_datetime" : ISODate("2015-10-21T14:11:40.671Z")
        },
        {
            "ieq_input_raw_value" : "110010000000001010001000000001100000000000000000",
            "sensor_datetime" : 1445429500.679,
            "db_datetime" : ISODate("2015-10-21T14:11:40.679Z")
        }
    ],
    "device_id" : ObjectId("560e197f99d094dea7023768")
}
 
If I execute following query:
db.compact_channel_data.find({},{'data.sensor_datetime': 1}).sort({'data.sensor_datetime': 1})
 
I receive following result:
/* 0 */
{
    "_id" : ObjectId("562786dc99d0948a7f1fb3de"),
    "data" : [
        {
            "sensor_datetime" : 1445429500.625
        },
        {
            "sensor_datetime" : 1445429500.66
        },
        {
            "sensor_datetime" : 1445429500.624
        },
        {
            "sensor_datetime" : 1445429500.659
        },
        {
            "sensor_datetime" : 1445429500.671
        },
        {
            "sensor_datetime" : 1445429500.679
        }
    ]
}
 
But there is problem because the value 1445429500.66 should be behind 1445429500.659.
 
The original value of 1445429500.66 (ISODate("2015-10-21T14:11:40.660Z") shall be behind 1445429500.659 ISODate("2015-10-21T14:11:40.659Z").

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