Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-43068

$meta 'sortKey' projection doesn't contain all fields sorted on

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Query
    • ALL
    • Hide

      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.

      Show
      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.

          Assignee:
          backlog-server-query Backlog - Query Team (Inactive)
          Reporter:
          carl.worley@mongodb.com Raiden Worley (Inactive)
          Votes:
          0 Vote for this issue
          Watchers:
          6 Start watching this issue

            Created:
            Updated:
            Resolved: