Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I store relational database structure in MongoDB

Tags:

mongodb

I am a java programmer and I want to use MongoDB in my project. I know that MongoDB is a document database but is there any way to store more fields for a record in this db ?

For example I have a table with 4 attributes (i.e. columns) name address age and dob so can I use or convert this structure to fit into MongoDB ?

like image 794
Vivek Avatar asked Dec 21 '25 23:12

Vivek


1 Answers

Of course.

MongoDB is a document-based database management system. If you are familiar with the JSON format, Mongo stores data in a very similar manner. For example, a sample document in a Mongo database may look like this:

Person {
    Name:
    {
       First : "John"
       Last : "Smith"
    }
    Address :
    {
       City : "Chicago"
       State : "Illinois"
       Street : "30 S. Michigan Avenue"
    }
} 

As you can see Mongo supports nested fields, so a particular field can contain one or more attributes. It is not key/value per se, as it is possible to nest document ad infinitum. Beware, however, that not all documents in a collection have to have the same structure. This is Mongo's biggest strength, but it can turn around and bite you if you're not careful.

like image 131
Ben Siver Avatar answered Dec 26 '25 12:12

Ben Siver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!