• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

how to get record from MongoDB

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad that it was useful!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/.
 
reply
    Bookmark Topic Watch Topic
  • New Topic