[SERVER-43068] $meta 'sortKey' projection doesn't contain all fields sorted on Created: 28/Aug/19  Updated: 27/Oct/23  Resolved: 28/Aug/19

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

Type: Bug Priority: Major - P3
Reporter: Raiden Worley (Inactive) Assignee: Backlog - Query Team (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query
Operating System: ALL
Steps To Reproduce:

Create a database where there are multiple fields:

> db.test.find()
{ "_id" : ObjectId("5d65896a4aea8c58f56007c2"), "a" : 1 }
{ "_id" : ObjectId("5d6589774aea8c58f56007c3"), "a" : 2 }
{ "_id" : ObjectId("5d65897e4aea8c58f56007c4"), "a" : 3 }
{ "_id" : ObjectId("5d6589864aea8c58f56007c5"), "a" : 4 }
{ "_id" : ObjectId("5d6591f747ccc30d70199b18"), "a" : { "x" : "z" } }
{ "_id" : ObjectId("5d6599ac47ccc30d70199b19"), "a" : 15, "b" : 99 }
{ "_id" : ObjectId("5d659a1347ccc30d70199b1a"), "a" : 15, "b" : 300 }

Create a sort command containing the "$meta" : "sortKey" projection:

> findCommand
{
	"find" : "test",
	"sort" : {
		"a" : -1,
		"b" : -1
	},
	"projection" : {
		"sortKey" : {
			"$meta" : "sortKey"
		}
	}
}

Now the fields in the returned documents are sorted as asked in the query, but the 'sortKey' field only has some of the information used to sort:

> db.runCommand(findCommand)
{
	"cursor" : {
		"firstBatch" : [
			{
				"_id" : ObjectId("5d6591f747ccc30d70199b18"),
				"a" : {
					"x" : "z"
				},
				"sortKey" : {
					"" : {
						"x" : "z"
					}
				}
			},
			{
				"_id" : ObjectId("5d659a1347ccc30d70199b1a"),
				"a" : 15,
				"b" : 300,
				"sortKey" : {
					"" : 15
				}
			},
			{
				"_id" : ObjectId("5d6599ac47ccc30d70199b19"),
				"a" : 15,
				"b" : 99,
				"sortKey" : {
					"" : 15
				}
			},
			{
				"_id" : ObjectId("5d6589864aea8c58f56007c5"),
				"a" : 4,
				"sortKey" : {
					"" : 4
				}
			},
			{
				"_id" : ObjectId("5d65897e4aea8c58f56007c4"),
				"a" : 3,
				"sortKey" : {
					"" : 3
				}
			},
			{
				"_id" : ObjectId("5d6589774aea8c58f56007c3"),
				"a" : 2,
				"sortKey" : {
					"" : 2
				}
			},
			{
				"_id" : ObjectId("5d65896a4aea8c58f56007c2"),
				"a" : 1,
				"sortKey" : {
					"" : 1
				}
			}
		],
		"id" : NumberLong(0),
		"ns" : "test.test"
	},
	"ok" : 1
}

Now the sort is occurring on both 'a' and 'b' but only the values of 'a' are included in the sortKey field.

Participants:

 Comments   
Comment by Raiden Worley (Inactive) [ 28/Aug/19 ]

bernard.gorman Yeah that perfectly addresses it! Thanks to both of you for taking the time to look at this, I learned a lot.

Comment by Bernard Gorman [ 28/Aug/19 ]

max.hirschhorn: yes, that's the conclusion we came to after discussing this ticket in the query channel. I didn't know we'd added a bsonObjToArray helper, very cool!

carl.worley, does this address your use-case? If so, I'll close the ticket.

Comment by Max Hirschhorn [ 28/Aug/19 ]

The output included in the description is just the behavior of how the mongo shell deals with BSON containing duplicate field names. The bsonObjToArray() function introduced in SERVER-42664 is specifically designed to address this shortcoming for testing purposes with how the sort key is currently represented.

Generated at Thu Feb 08 05:02:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.