Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get record from MongoDB

 
malik ge
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am a new-bee to mongodb and first time using it with Java.

I am creating a simple logIn page.
The following code run fine, and get all the record matching the "firstName" and "password".



While using Java with MySql, by doing:

I can get the values from MySql database and stores it in some variables.
With mongoDB how I get each value separately from a document and store it in variable?
Thanks
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember a query in MongoDB returns one or more documents in BSON format (like JSON). I'm new to MongoDB as well, but I think the individual documents are instances of BasicDBObject, which inherits from BasicBSONObject - this has various "get" methods to get individual elements by their key etc.

Check out the MongoDB Java API documentation.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3165
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you do a DBCursor.next() you can get hold of the DBObject instance. Something like:

you then have to correspondingly cast to the required type. I am not sure if you can cast the return of cursor.next() directly into BasicDBObject and then make use of getInt, getBoolean, getString method provided by BasicDBObject. Something like:


If you are familiar with ORM frameworks then you can try out Morphia which helps in making these operations a bit simpler.

As Chris pointed out, the MongoDB Java documentation should help you a lot.
 
malik ge
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By doing this, I am able to get the records from the mongodb.



But how can I get the "_id" object from the database.

This shows the all the record:
System.out.println(cursor.next());
{ "_id" : { "$oid" : "4fc68190a09ef7490a0b3b40"} , "firstName" : "Some First Name" , "lastName" : "Some Last Name"}


But when I try to get the id, it gives me "null" value
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3165
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to have a look at ObjectId class from the MongoDB documentation.

Recently I wrote an article which on a blog- Using Morphia- Java Library for MongoDB. Here I compared using MongoDB driver and Morphia API.

PS: I was reluctant to share this link in my first reply tending it to be Blatant Advertisement. But I have code samples there which show you how to retrieve the "id" field from Mongo's BSON
 
malik ge
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:You need to have a look at ObjectId class from the MongoDB documentation.

Recently I wrote an article which on a blog- Using Morphia- Java Library for MongoDB. Here I compared using MongoDB driver and Morphia API.

PS: I was reluctant to share this link in my first reply tending it to be Blatant Advertisement. But I have code samples there which show you how to retrieve the "id" field from Mongo's BSON


Thanks a lot man.
That is exactly what I am looking for.
It has not only clarify my current problem, but also helped me understanding some other useful things.
Thanks again.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3165
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad that it was useful!
 
Johnc Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I know it’s too late, I think your query will be solved now. I would like to share a website, where discussions are carry on related to MongoDB and many other. Here is the link: https://zappysys.com/blog/ssis-loading-data-into-mongodb-upsert-update-delete-insert/.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic