MongoDB 和 Java 对象

发布于 2024-09-28 14:35:18 字数 1412 浏览 1 评论 0原文

我的 Mongo 数据库中存储了以下 MongoDB 对象:

array (
  '_id' => new MongoId("4cc183de48aa4c7708000000"),
  'vehicleMake' => 'Volkswagen',
  'vehicleModel' => 'Carrier',
  'vehicleNumPassengers' => '7',
  'vehicleReg' => '07-D-748393',
  'coords' => 
  array (
    0 => 
    array (
      'lat' => '53.2594946',
      'lng' => '-6.1760780',
      'timestamp' => '12345678',
    ),
    1 => 
    array (
      'lat' => '53.8994946',
      'lng' => '-6.1460780',
      'timestamp' => '12345678',
    ),
  ),
)

在 java 中,我可以使用以下代码访问字段 'vehicleMake'、'vehicleModel'、'vehicleNumPassengers'、'vehicleReg':

Mongo mongo=new Mongo();
DB db=mongo.getDB("garage");
DBCollection cllctn=db.getCollection("drivers");
DBCursor allDrivers=cllctn.find();

while(allDrivers.hasNext()){
    DBObject driver=allDrivers.next();
    String vehicleMake=driver.get("smsVerified").toString();
    String vehicleModel=driver.get("vehicleModel").toString();
    String vehicleNumPassengers=driver.get("vehicleNumPassengers").toString();
    String vehicleReg=driver.get("vehicleReg").toString();
    //
    //How do I access 'lat', 'lon' and 'timestamp' here????
    //    
}

我的问题是:如何访问MongoDB 数组中的“lat”、“lon”和“timestamp”值?我完全被难住了,在 mongodb 网站或 Google 上找不到任何东西。

我尝试过使用 BasicDBObject 但似乎无法让任何东西发挥作用。

非常感谢任何帮助。

预先非常感谢,

I have the following MongoDB object stored in my Mongo database:

array (
  '_id' => new MongoId("4cc183de48aa4c7708000000"),
  'vehicleMake' => 'Volkswagen',
  'vehicleModel' => 'Carrier',
  'vehicleNumPassengers' => '7',
  'vehicleReg' => '07-D-748393',
  'coords' => 
  array (
    0 => 
    array (
      'lat' => '53.2594946',
      'lng' => '-6.1760780',
      'timestamp' => '12345678',
    ),
    1 => 
    array (
      'lat' => '53.8994946',
      'lng' => '-6.1460780',
      'timestamp' => '12345678',
    ),
  ),
)

And in java, I can access the fields 'vehicleMake', 'vehicleModel', 'vehicleNumPassengers', 'vehicleReg' fine using the following code:

Mongo mongo=new Mongo();
DB db=mongo.getDB("garage");
DBCollection cllctn=db.getCollection("drivers");
DBCursor allDrivers=cllctn.find();

while(allDrivers.hasNext()){
    DBObject driver=allDrivers.next();
    String vehicleMake=driver.get("smsVerified").toString();
    String vehicleModel=driver.get("vehicleModel").toString();
    String vehicleNumPassengers=driver.get("vehicleNumPassengers").toString();
    String vehicleReg=driver.get("vehicleReg").toString();
    //
    //How do I access 'lat', 'lon' and 'timestamp' here????
    //    
}

My question is: how do I access the 'lat', 'lon' and 'timestamp' values inside the MongoDB array? I'm completely stumped and can't find anything on the mongodb website or on Google.

I've tried messing around with BasicDBObject but can't seem to get anything to work.

Any help greatly appreciated.

Many thanks in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2024-10-05 14:35:18

你必须这样做:

BasicDBObject query = new BasicDBObject();
query.put("array.lat");
collection.find(query);

或者

这样做:

System.out.println(cursor.next().get("coords").get("lat"));

你还必须向它添加一个 BSONObject 强制转换。

编辑:我认为第一种方法是不正确的。您还需要一个值对来配合。

you have to do:

BasicDBObject query = new BasicDBObject();
query.put("array.lat");
collection.find(query);

or

do this:

System.out.println(cursor.next().get("coords").get("lat"));

you will also have to add a BSONObject cast to it.

EDIT: I Think the first method is incorrect. you also need a value pair to go along.

丘比特射中我 2024-10-05 14:35:18

@Kaustubh 我现在开始工作了...

BSONObject coords=(BSONObject)driver.get("coords");
BSONObject coords_first=(BSONObject) coords.get("0");
String lat=coords_first.get("lat").toString();
String lng=coords_first.get("lng").toString();
System.out.println(lat+"---"+lng);

诀窍是您正确提到的 BSONObject cast

@Kaustubh I got it working now...

BSONObject coords=(BSONObject)driver.get("coords");
BSONObject coords_first=(BSONObject) coords.get("0");
String lat=coords_first.get("lat").toString();
String lng=coords_first.get("lng").toString();
System.out.println(lat+"---"+lng);

The trick was the BSONObject cast which you correctly mentioned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文