【问题标题】:How to retrive Integer and Timestamp value from array stored in MongoDB with Java Driver如何使用 Java 驱动程序从存储在 MongoDB 中的数组中检索整数和时间戳值
【发布时间】:2017-05-06 17:47:50
【问题描述】:

我用 Java 编写了一个程序,它生成值并将它们以下列形式存储在 MongoDB 数据库中:

{
    "_id" : 0,
    "destination" : "destination_1",
    "origin" : "origin1",
    "duration_value" : [
            5,
            5,
            12
    ],
    "duration_text" : [
            null,
            null,
            null
    ],
    "timestamp" : [
            ISODate("2017-05-03T15:17:12.570Z"),
            ISODate("2017-05-03T15:17:39.363Z"),
            ISODate("2017-05-06T17:16:43.925Z")
    ]}

如您所见,共有三个数组。在duration_value 数组中,它们将始终存储在整数值中,而在timestamp 数组中,它们将始终存储time stamp 值。 我现在需要检索存储在duration_value 中的整数值,以便进行计算并检索在timestamp 数组中输入的值,而不是DBObjectBasicDBObject,而是作为Timestamp 中的对象具有可比性的类,能够进行其他操作。我该怎么办?

到目前为止,我已经能够像 DBObject 一样从数据库中检索元素,而不是使用它们的原始类型,我什至无法在没有错误的情况下转换 DBObject 错误。我使用了以下代码:

    MongoClient mongo = null;
    DBCursor cursor = null;


    try {
        mongo = new MongoClient ("localhost", 27017);
        DB db = mongo.getDB("testdb2");

        DBCollection table = db.getCollection("user");
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("_id", 0);
        cursor = table.find(searchQuery);

        DBObject resultElement = cursor.next(); 
        List<DBObject> list = (List<DBObject>)resultElement.get("timestamp");             

        for(int i = 0; i < list.size(); i++){

            System.out.println("indice: " + i + " contenuto: " + list.get(i));
        }


    }
    catch(Exception e){ 
        System.out.println("error : " + e.getMessage());
        System.out.println("error : " + e.getCause());
    }
    finally{
        cursor.close();
    }

对不起我的英语,谢谢你的帮助。

【问题讨论】:

  • 你试过了吗? Date date = (Date) list.get(i); Mongo java 驱动将 mongo 时间戳类型映射到 java.util.Date 类型。您可以在 Mongo 3.x 驱动程序版本中使用新的 DocumentMongoCollection api 类型安全变体。
  • 老实说,我用整数尝试了它,因为我需要它用于另一个数组,而且因为我什至无法转换那些我觉得很不舒服,所以我没有尝试时间戳。我现在尝试了它并且它有效,现在我必须了解如何将另一个数组转换为整数。你解决了我一半的问题。谢谢。

标签: java arrays mongodb mongo-java-driver


【解决方案1】:

将您的代码更新到下面。使用 findOne 而不是 findid 上进行搜索。

MongoDB 将时间戳映射到 java.util.Date,将编号映射到 java.lang.Double

 DBObject result = table.findOne(searchQuery);

 BasicDBList number = (BasicDBList)result.get("duration_value");
 BasicDBList timestamp = (BasicDBList)result.get("timestamp");

 int integer = ((Number)number.get(i)).intValue();
 Date date = (Date) timestamp.get(i);

【讨论】:

    【解决方案2】:

    基于 Java Driver API,您可以从 DBObject 获取基本的 Java 类型。

    您不能直接从 DBObject 获取 Timestamp 实例。

    BasicDBObject 的 API 文档: http://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/BasicDBObject.html

    继承的 BasicBSONObject 的 API 文档:http://mongodb.github.io/mongo-java-driver/3.4/javadoc/org/bson/BasicBSONObject.html

    还有其他可用的 Java MongoDB 库。其中之一是 MongoJack 是基于带有 Jackson 库的 Java 驱动程序。它支持开箱即用的 JSON 到 Java 对象的映射。

    【讨论】:

    • 感谢您的帮助。
    • 欢迎来到 Stack Overflow!虽然这在理论上可以回答这个问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
    猜你喜欢
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多