[JAVA-490] Check value type that passed to a sort method Created: 14/Dec/11  Updated: 25/Jun/13  Resolved: 25/Jun/13

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Uladzimir Mihura Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Please, take a look at the following code. I think that only int -1 or 1 should be allowed here.

public static void main(String... args) throws UnknownHostException {
    Mongo m = new Mongo();
    m.setWriteConcern(WriteConcern.SAFE);
    DB db = m.getDB("test");
    db.dropDatabase();
    DBCollection collection = db.getCollection("tmp");
    //random docs insert
    for (int i = 0; i < 10; i++) {
        DBObject object = new BasicDBObject("_id", i);
        object.put("length", (int) (Math.random() * 100));
        collection.insert(object);
    }
 
    print(collection.find().toArray());
    //valid value
    BasicDBObject obj = new BasicDBObject().append("length", 1);
    print(collection.find().sort(obj).toArray());
    //string value
    obj.put("length", "randomString");
    print(collection.find().sort(obj).toArray());
    //object value
    obj.put("length", new BasicDBObject());
    print(collection.find().sort(obj).toArray());
}
 
private static void print(List<DBObject> list){
    System.out.println("--size "+list.size());
    for(DBObject o : list){
        System.out.println(o);
    }
    System.out.println("\n");
}



 Comments   
Comment by Jeffrey Yemin [ 25/Jun/13 ]

We can use helpers to guide users in the right direction.

Comment by Scott Hernandez (Inactive) [ 29/Dec/11 ]

Vova, are these docs not sufficient?

http://www.mongodb.org/display/DOCS/Indexes
http://www.mongodb.org/display/DOCS/Geospatial+Indexing
http://www.mongodb.org/display/DOCS/Geospatial+Haystack+Indexing

Comment by Uladzimir Mihura [ 29/Dec/11 ]

Hi Scott,

Can you please clarify? I haven't found any other information about that.
I see that index is defined by field for index(String,Object,Array, etc...), direction(-1,1,2d) and options(sparse,unique).

-vova

Comment by Scott Hernandez (Inactive) [ 29/Dec/11 ]

The server is in charge of validating... there are many more types of indexes than just ascending/descending.

Comment by Uladzimir Mihura [ 29/Dec/11 ]

Similar situation when adding indexes. In documentation:

When creating an index, the number associated with a key specifies the direction of the index, so it should always be 1 (ascending) or -1 (descending).

But in code:

Mongo m = new Mongo();
DBCollection c = m.getDB("test").getCollection("tmp3");
c.ensureIndex(BasicDBObjectBuilder.start()
        .add("test",-10)
        .add("tes2",1000)
        .get());
//no exception

Comment by Brendan W. McAdams [ 14/Dec/11 ]

We should restrict the use of anything but int. This is likely an issue that will require generics to filter what is passed in the DBObject argument however.

Comment by Brendan W. McAdams [ 14/Dec/11 ]

Technically, any positive value is "Ascending sort" and any negative value is "descending sort". That's the server-side behavior.

Comment by Alex Litvinok [ 14/Dec/11 ]

In PHP works like Java. If the direction not equal -1, it will be used as 1.

Comment by Vitali Ausianik [ 14/Dec/11 ]

In Python it works fine. Function Cursor.sort() takes either a single key and a direction, or a list of (key, direction) pairs. The key(s) must be an instance of (str, unicode), and the direction(s) must be one of (1,-1). If direction not in (1, -1) - exception will be generated.

Generated at Thu Feb 08 08:52:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.