• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MongDB Async Java Driver retrieving sub documents!

 
nick doran
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've only recently started experimenting with MongoDB via Async Driver. Here's the Maven dependency below.

So, I'm having an issue reading from a returned Document. I'm not exactly sure if it's the way I'm putting the Document into the database, or the way I'm wanting to read from it.

The way I'm putting the document inside the database.

That appears to work fine, it successfully injects into the database and looks like this;

Now, I'm wanting to retrieve the player name data so.. I do this;

To my understanding this checks, the 'uuid' field under the index of 'info'? This returns fine, it returns the document or what I think is the document? But in the asynchronous callback, "specified=" is always null.. And it should be returning the String 'nick'?
Any help would be appreciated! Thanks -nick.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't used this driver yet myself, but maybe the driver quick tour will help.

PS Don't forget to put code tags around the code snippets in your post to make it easier to read. I've added them for you here.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just noticed that your "player" field - like "uuid" - is nested under "info", so should you be reading "info.player" instead (or however you express the nested fields in this driver)?
 
nick doran
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chris webster wrote:Just noticed that your "player" field - like "uuid" - is nested under "info", so should you be reading "info.player" instead (or however you express the nested fields in this driver)?


This is a very strange issue I'm having, as for the documentation. I've read that page in and out several times, it doesn't show retrieving fields from sub documents.
So this code below so what I use to find the document, it works. It finds the document and passes it to the Async callback.


This, the async callback shows the entire document via System.out.println(document.ToJson());.
But.. for whatever reason, document.get(object), always returns null..


I've stored and retrieved data fine when I didn't have it tested under something 'info' for instance, but I don't want to get rid of my indexes ;/
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wondering, but is "info" effectively another document? If so, then maybe you need to fetch "info" as a document, then do the "get(...)" on this (sub-)document?

It#s a while since I used Java with MongoDB so I'm kind of rusty on how these things hang together!
 
nick doran
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chris webster wrote:Just wondering, but is "info" effectively another document? If so, then maybe you need to fetch "info" as a document, then do the "get(...)" on this (sub-)document?

It#s a while since I used Java with MongoDB so I'm kind of rusty on how these things hang together!


Yes, 'info' is a[n] embedded document.
To retrieve the fields/data from the embedded document I use;

Which will find a document that contains info.player with value of 'nick' and return all the fields inside embedded document 'info'.
The query seems to fetch the document fine, I can also update fields like info.credits. But I can't document.get("info.player") or any other data inside the 'info' document. I'm just so confused, I've literally posted everywhere, no one in the Support IRC. No replies on StackOverflow. ;/
 
nick doran
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem Solved.

Document doc = new Document("info", new Document("player", "nick")
.append("uuid", "29412")
.append("credits", 100)
.append("karma", 0));

System.out.println(doc.toJson());
Object info = doc.get("info");
System.out.println(((Document) info).toJson());
System.out.println(((Document) info).get("player"));

Thanks to support over at http://mongolabs.com
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting your solution, Nick. Have a cow!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic