我在mongoDb
中有以下文件{
"_id" : 0,
"GroupUuid" : 0,
"GroupActivationDate" : "2015-08-01T00:00:00.000+05:00",
"PurchaseDate" : "2015-08-24T12:42:24.380+05:00",
"GroupExpirationDate" : "2015-08-28T00:00:00.000+05:00",
"HousefullDate" : "0001-01-01T01:01:00.000+04:28:12",
"ArtShare" : {
"TotalArtShares" : 0,
"pricePerShare" : 0,
"ArtworkUuid" : 12,
"AvailableShares" : 0,
"SoldShares" : 0
}
}
当我这样做时db.groupBuying.find({"ArtShare.TotalArtShares":0}).pretty()
上面显示的文件
现在我想使用我正在做的代码
来做到这一点val cursor=collection.find()
var obj=new BasicDBObject
while(cursor.hasNext)
{
obj=cursor.next().asInstanceOf[BasicDBObject]
var id=obj.getString("ArtShare.TotalArtShares").toInt
log.info("TotalArtShares "+id)
}
}
但是抛出异常后
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:247)
at scala.collection.immutable.StringOps.toInt(StringOps.scala:30)
at models.groupbuyingmodels.groupbuyingMongoReadWrite.GroupBuyingStore.write(GroupBuyingStore.scala:43)
at models.groupbuyingmodels.groupbuyingMongoReadWrite.GroupBuyingWriteMongoActor$$anonfun$receive$1.applyOrElse(GroupBuyingWriteMongoActor.scala:27)
at akka.actor.Actor$class.aroundReceive(Actor.scala:465)
at models.groupbuyingmodels.groupbuyingMongoReadWrite.GroupBuyingWriteMongoActor.aroundReceive(GroupBuyingWriteMongoActor.scala:15)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)
at akka.actor.ActorCell.invoke(ActorCell.scala:487)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238)
at akka.dispatch.Mailbox.run(Mailbox.scala:220)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
13:08:55.166 1652348 [ArteciateActorSystem-akka.actor.GroupBuyingWriteMongoActor-dispatcher-6] controller ERROR - printStackTrace()
当我这样做时
var obj=new BasicDBObject
while(cursor.hasNext)
{
//var cur=cursor.next()
obj=cursor.next().asInstanceOf[BasicDBObject]
var id=obj.getString("GroupUuid").toInt
log.info("GroupUuid"+id)
}
}
然后在控制台上打印GroupUuid
的值
我如何获取TotalArtShares
价值请帮助我,我做错了,请指导我
答案 0 :(得分:1)
我现在没有Scala的任何东西,但一般的Java技术是首先通过它的顶级键获取文档,然后从底层对象访问属性:
MongoClient client = new MongoClient(new ServerAddress("192.168.2.4", 27017));
MongoDatabase mongoDatabase = client.getDatabase("test");
MongoCollection<Document> trash = mongoDatabase.getCollection("trash");
MongoCursor<Document> cursor = trash.find().iterator();
// { "_id" : ObjectId("55dad435d622c0483e94d3b2"), "something" : { "nested" : 1 } }
while ( cursor.hasNext() ) {
Document doc = cursor.next();
Document something = doc.get("something", Document.class);
Double nested = something.getDouble("nested");
System.out.println(nested);
}
"dot notation"概念是MongoDB在内部处理嵌入数据的方式,并不一定将您选择的语言翻译为在repsonses中访问返回的数据结构的方法。
一旦将事物作为本机对象返回,则由语言构成如何操作它们,并且“点符号”之类的内容不再适用,除非您正在构建查询。
答案 1 :(得分:1)
我假设您正在使用来自scala的java mongodb驱动程序,
val cursor=collection.find(new BasicDBObject("ArtShare.TotalArtShares", 0))
var obj: BasicDBObject= _
while(cursor.hasNext) {
obj = cursor.next().asInstanceOf[BasicDBObject]
val artShare = obj.get("ArtShare").asInstanceOf[BasicDBObject]
val id = artShare.getInt("TotalArtShares")
log.info("TotalArtShares " + id)
}
第一个语句是查询,然后在while循环中检索嵌入文档“ArtShare”并获取字段“TotalArtShares”。
我建议你使用cashbah,这是scala包装器构建在java驱动程序之上,它暴露scala api。