简体   繁体   中英

Can I store relational database structure in 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 (ie columns) name address age and dob so can I use or convert this structure to fit into MongoDB?

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.

Just to add to the correct (+1) answers from the previous two posters...

There's a difference between "relational database structure" and storing additional attributes. Relational implies that you're modeling relationships between things in your database, like customers and the orders that each customer has.

MongoDB allows you to store documents with many attributes. It does not provide much support to mange relationships between documents.

Actually, MongoDB is not a traditional key/value database but a document database. A document is a JSON object, which can contain any number of fields, or even nested documents. So yes, you can easily store multiple fields for each record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM