3

I have the following document in 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
    }
}

when I do this db.groupBuying.find({"ArtShare.TotalArtShares":0}).pretty() above document displayed

Now I want to do this using code I am doing like this

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)
     }
   }

But following exception thrown

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()

When I do this

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)
     }
   }

Then the value of GroupUuid is printed on console how can I fetch TotalArtShares value please help me and where I am doing wrong please guide me

2 Answers 2

4

I don't have anything up for Scala right now, but the general Java technique is to first get the document by it's top level key, and then access the properties from the underlying object:

    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);
    }

The "dot notation" concept is how MongoDB handles embedded data internally, and does not necessarily translate your you chosen language as a means to access the returned data structures in the repsonses.

Once things are returned as native objects, then it is up to the language contructs to how you manipulate them, and things like "dot notation" no longer apply, unless you are contructing queries.

Sign up to request clarification or add additional context in comments.

Comments

1

I assume that you are using java mongodb driver from scala,

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)

  }

First statement is query, and then in a while loop I retrieve embedded document "ArtShare" and get field "TotalArtShares".

I would recommend you to use cashbah which is scala wrapper build on top of java driver, it expose scala api.

2 Comments

i need to fetch the whole document so i need find() not find(new BasicDBObject("ArtShare.TotalArtShares", 0)) any way thanks alot for the help
this query statement will return you the whole document, but this document must meet that criteria, it's equivalent of your javascript code. From this code I see only that you are using java api , I do not see casbah api here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.