我的Mongo收藏Nee2500AS1有2行:
db.Nee2500AS1.find()
{ "_id" : ObjectId("52ce3ff4c56df46f9defca62"), "asset" : "Nee2500AS1", "SaltRejection" : "82%", "SaltPassage" : "18%", "Recovery" : "56.33%"}
{ "_id" : ObjectId("52ce4013c56df46f9defca63"), "asset" : "Nee2500AS1", "SaltRejection" : "182%", "SaltPassage" : "18%", "Recovery" : "56.33%"}可以查询db.Nee2500AS1.find("Latest Timestamp")吗?
_id包含时间戳,对吗?
我的java代码是:
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("TESTDoc");
DBObject allQuery = new BasicDBObject();
DBCollection collection = db.getCollection("Nee2500AS1");
DBCursor cursor = collection.find(allQuery);如何修改我的java代码以获取最新添加的行?
发布于 2014-01-09 07:50:31
很简单..。
DBCollection collection = db.getCollection("Nee2500AS1");
DBCursor cursor = collection.find(allQuery).sort( new BasicDBObject( "_id" , -1 ));
DBObject resultElement = null;
resultElement = cursor.next();在这里,.sort(新的BasicDBObject( "_id“,0))返回所需的结果。
发布于 2014-01-09 07:27:54
使用排序和限制的组合参见DBCursor javadoc。
如果要按_id以外的其他内容进行排序,则可能要添加索引以提高性能。
collection.find(allQuery).sort(new BasicDBObject("_id", 1)).limit(1);https://stackoverflow.com/questions/21013401
复制相似问题