[SERVER-27492] Support $natural sorts on views Created: 21/Dec/16  Updated: 06/Dec/22  Resolved: 03/Jan/17

Status: Closed
Project: Core Server
Component/s: Aggregation Framework, Querying
Affects Version/s: 3.4.0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Nathan Fan [X] Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Fix Votes: 0
Labels: read-only-views
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Server 3.4.0


Issue Links:
Related
related to SERVER-26960 Consider improving error message when... Closed
is related to SERVER-7944 add index hint support for operations... Closed
Assigned Teams:
Query
Participants:

 Description   

In the code I tried to treat Views the same as collections. We applied .sort({$natural:1}) to all the queries. For normal collections the driver worked as before, but the sort failed on Views retrieval.

The example shown below:

        MongoCollection collection = client.getDatabase("test").getCollection("managementFeedback");
        FindIterable cursor = collection.find()
                .sort(new BsonDocument("$natural", new BsonInt32(1))).limit(2);
        MongoCursor<BsonDocument> document = cursor.iterator();

This returned:

Exception in thread "main" com.mongodb.MongoQueryException: Query failed with error code 16410 and error message 'FieldPath field names may not start with '$'.' on server 192.168.203.167:27017
	at com.mongodb.operation.FindOperation$1.call(FindOperation.java:521)
	at com.mongodb.operation.FindOperation$1.call(FindOperation.java:510)
	at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:431)
	at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:404)
	at com.mongodb.operation.FindOperation.execute(FindOperation.java:510)
	at com.mongodb.operation.FindOperation.execute(FindOperation.java:81)
	at com.mongodb.Mongo.execute(Mongo.java:836)
	at com.mongodb.Mongo$2.execute(Mongo.java:823)
	at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
	at com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:151)
	at test.Test.main(Test.java:55)

Is it the way I dealt with views wrongly?
If I should treat views specially (views are not sortable or already sorted), do I have a way to figure out if the collection is a collection or a view other than accessing the system.views collection?

P.S. mongoclient / command line (mongo) having the same problem as Java driver:

Error: error: {
	"ok" : 0,
	"errmsg" : "FieldPath field names may not start with '$'.",
	"code" : 16410,
	"codeName" : "Location16410"
}

Thanks for helping guys.



 Comments   
Comment by Charlie Swanson [ 03/Jan/17 ]

After review by the Query team, we have decided that $natural is more of a hint specification than a sort specification, and doesn't make sense in the middle of a pipeline. Allowing a $natural sort against a view may require adding that $sort after a $group stage or some other stage which does not preserve the order of documents. A $natural sort specification means to order the results in the order of a collection scan, which cannot be easily recreated at that point.

Adding hint support to the aggregation pipeline is tracked via SERVER-7944. I would encourage you to watch that ticket for updates. Once that is done, your requested find operation can be accomplished with a .hint() instead of a .sort().

Comment by Nathan Fan [X] [ 21/Dec/16 ]

Thanks for the clarification
I will keep watching this issue.

Comment by Kyle Suarez [ 21/Dec/16 ]

Hi NathanFan, it's true that $natural sorts do not work with views. This is because all views are implemented as aggregation pipelines, and the aggregation system does not support $natural sorts. I've converted this ticket into a feature request to support these sorts on views and sent it to the query team for consideration. Please continue to watch this ticket for updates.

Do I have a way to figure out if the collection is a collection or a view other than accessing the system.views collection?

You can either query the system.views collection, or check the output of the listCollections command, which gives output similar to this:

{
        "name" : "myCollection",
        "type" : "collection",
        "options" : {
 
        },
        "info" : {
                "readOnly" : false
        },
        "idIndex" : {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.collection"
        }
},
{
        "name" : "myView",
        "type" : "view",
        "options" : {
                "viewOn" : "collection",
                "pipeline" : [ ]
        },
        "info" : {
                "readOnly" : true
        }
}

Regards,
Kyle

Comment by Jeffrey Yemin [ 21/Dec/16 ]

Hi Nathan,

Since this issue is not specific to the Java driver, I took the liberty of moving it to the Core Server project.

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