Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL table to nosql (MongoDB) - easy example

I have some problems to understand nosql. Im using mongodb and java and would like to create something like that: a table (persons) with a column for name (as string), age (as integer), married (boolean). In a normal sql it would be easy... but how to go on with mongodb and java?

Ok stuff I know: a table in mongodb is a collection and a column is a BSON field. I would start like this

        Mongo m = new Mongo();
        DB db = m.getDB("myDatabase");
        DBCollection col = db.getCollection("Persons");
        BasicDBObject doc = new BasicDBObject();
        doc.put("something?", "something?");
        col.insert(doc);

the first 3 steps are easy. I have my collection (table), I should make the BSON fields (columns) name, age, married. But how? I know the put() method, but what should I put in? And if I have the construct, I would like to add some "persons".

Any ideas? Thank you

like image 513
Golem Avatar asked Nov 13 '11 23:11

Golem


People also ask

How do I convert SQL to NoSQL?

When migrating from SQL to NoSQL, the primary key in the relational table becomes the partition key in the NoSQL table. If the RDBMS table must be joined to additional tables to retrieve the business object, those closely related tables should combine into a single NoSQL table.

How do I transfer data from SQL Server to MongoDB?

Open SQL to MongoDB MigrationClick on the SQL Migration button in the toolbar, or right-click into a server, database or collection in the Connection Tree and select the SQL Migration option. Then select SQL → MongoDB Migration. This will open a new tab where you can configure and execute the import.


1 Answers

You should try to get rid of thinking about columns with MongoDB. It is schemaless so every document may have different set of fields even in same collection so thinking fields are columns may be misleading.

I recommend going through the official MongoDB Java tutorial HERE.

You should be able to do something like this:

doc.put("name", "John");
doc.put("age", 30);
doc.put("married", false);
like image 145
Lycha Avatar answered Sep 22 '22 09:09

Lycha