android - 从由 toArray() 转换的 mongodb 查询中获取字段值

标签 android mongodb

List<?> records = collection.find(gtQuery).toArray();
System.out.println(records);
//Output
 [{ "_id" : { "$oid" : "52aad1d3e6268005b08b3276"} , "dealer_id" : "1003" , "title" : "Airtel" , "description" : "Get free talktime on topup of Rs.100" , "expiration" : { "$date" : "2014-01-01T05:00:00.000Z"}}, { "_id" : { "$oid" : "52aad1dbe6268005b08b3277"} , "dealer_id" : "1002" , "title" : "Vodafone" , "description" : "Make your own plan for you" , "expiration" : { "$date" : "2014-01-11T05:00:00.000Z"}}, { "_id" : { "$oid" : "52aad1e1e6268005b08b3278"} , "dealer_id" : "1001" , "title" : "BSNL" , "description" : "We don`t do anything" , "expiration" : { "$date" : "2015-01-01T05:00:00.000Z"}}, { "_id" : { "$oid" : "52aad1f1e6268005b08b3279"} , "dealer_id" : "1004" , "title" : "Indigo" , "description" : "Get Rs.500 off on your next flight" , "expiration" : { "$date" : "2014-06-01T04:00:00.000Z"}}, { "_id" : { "$oid" : "52aad1fbe6268005b08b327a"} , "dealer_id" : "1005" , "title" : "Flipkart" , "description" : "Free shipping on order of Rs.200 and above" , "expiration" : { "$date" : "2014-02-01T05:00:00.000Z"}}, ]

我可以使用records.get(index) 获取个人记录。但是现在如何获取特定字段的值,例如 dealer_id、title、description 等的值?

最佳答案

你可以这样做:

DBCursor cur = collection.find(gtQuery);
while(cur.hasNext()) {
   DBObject obj = cur.next();
   String dealerId = (String) obj.get("dealer_id");
   String title = (String) obj.get("title");
   String description = (String) obj.get("description");
}

如果你想获取一个列表并遍历它,那么你可以按如下方式进行:

List<DBObject> records = coll.find().toArray();
for (DBObject record : records) {
   String dealerId = (String) record.get("dealer_id");
   String title = (String) record.get("title");
   String description = (String) record.get("description");
}

关于android - 从由 toArray() 转换的 mongodb 查询中获取字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20755817/

相关文章:

android - BroadcastReceiver 异步 onReceive

mongodb - 在mongodb中查询不同的多级关系

mysql - 在 MySQL 中创建用户帐户时如何有效更新 mongodb?

javascript - 了解 ReactJS 和 NodeJS 应用程序如何工作

android - Android 语音识别将什么格式的语音发送到云端?

Android:将 Canvas 绘制到 ImageView

android - 使用 rxjava 进行 fragment 间通信

javascript - 实时更新性能问题

node.js - Mongoose 按分数索引

java - 深度 sleep 连接蓝牙设备失败