14

コレクション:プログラム

{ "_id" : "ABC", "defaultDirectory" : "abc", "defaultRecvDirectory" : "abc" }
{ "_id" : "RAS", "defaultRecvDirectory" : "recv/ras" }
{ "_id" : "SND", "defaultSendDirectory" : "send/snd" }

mongoコンソールの場合:

db.progs.find({"_id":{"$lt":"ZZZZZZZZZ"}}).sort({"_id":-1}).limit(1);

==>    { "_id" : "SND", "defaultSendDirectory" : "send/snd" }

Javaの場合:

    BasicDBObject query = new BasicDBObject();
    query.put("_id", new BasicDBObject("$lt", "ZZZZZZZZZZ"));
    DBCursor cursor = collection.find(query).sort(new BasicDBObject("_id","-1")).limit(1);
    for (DBObject dbObject : cursor) {
        System.out.println(dbObject);
    }

==>    { "_id" : "ABC", "defaultSendDirectory" : "abc", "defaultRecvDirectory" : "abc" }

誰かが違いを説明できますか?

4

4 に答える 4

27

並べ替えから引用符を削除し"-1"ます。

DBCursor cursor = collection.find(query).sort(new BasicDBObject("_id",-1)).limit(1);

com.mongodb.operation.OrderByまたは、1 / -1 をハードコーディングする代わりに、Mongodb ASC/DESC 定数を使用します。

例:

DBCursor cursor = collection.find(query).sort(new BasicDBObject("_id", OrderBy.DESC.getIntRepresentation())).limit(1);
于 2012-12-21T16:56:48.183 に答える