MongoDB 和 Java 对象
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你必须这样做:
或者
这样做:
你还必须向它添加一个 BSONObject 强制转换。
编辑:我认为第一种方法是不正确的。您还需要一个值对来配合。
you have to do:
or
do this:
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.
@Kaustubh 我现在开始工作了...
诀窍是您正确提到的 BSONObject cast 。
@Kaustubh I got it working now...
The trick was the BSONObject cast which you correctly mentioned.