I'm trying to understand how mongodb works and I have some questions.
I understand how to delete, insert, update and select but I have some "best practice questions"
1) Did we have to create an index or we can just use the _id wich is auto generated ?
2) If I have for exemple 2 kind of objects (cars and drivers) with a n-n relation between. I have to get 3 collections(car, driver and a collection witch link the two others) ?
3) To rebuild my objects I have to parse my json with the JSON object ?
Thanks for your help
Three good questions. I'll answer each in turn.
1) Did we have to create an index or we can just use the _id wich is
auto generated ?
You should definitely try and (re)use the _id index. This usually means moving one of the unique fields (or primary key, in RDMS speak) in your domain object to be mapped to the _id field. You do have to be careful that the field does not get to large if you will be sharding but that is a separate question to answer.
2) If I have for exemple 2 kind of objects (cars and drivers) with a
n-n relation between. I have to get 3 collections(car, driver and a
collection witch link the two others) ?
No! You only need two collections. One for the cars and one for the drivers. The "join table" is pulled into each of the collections as DBRefs.
Each car document will contain an array of DBRef or document references. Those references contain the database name (optional), collection name and _id for a driver document. You will have a similar set of DBRefs in the driver document to each of the driven cars.
Most of the drivers have support for creating and de-referencing these document references.
3) To rebuild my objects I have to parse my json with the JSON object?
MongoDB lingua franca is actually BSON. You can think of BSON as a typed, binary, easily parsed version of JSON. Most of the drivers have some capability to convert from JSON to their representation of BSON and back. If you are developing in Java then the 10gen driver's utility class is JSON. For the Asynchronous driver it is called Json.
Having said that, unless your data is already JSON I would not convert your data to JSON to convert it to BSON and back again. Instead either look for a ODM (Object-Document-Mapper) for your language of choice or perform the translation for your domain object to the driver's BSON representation directly.
HTH-
Rob.
Related
I have using maria db to store dynamic columns.In sql Query it is there in the maria db documentation. https://mariadb.com/kb/en/library/dynamic-columns/ .
But i couldn't find a spring boot jpa implementation.I have tried jpa native query.I am storing a Json in this dynamic column which is data type of Blob in maria db.But it is very tough because i couldn't find a way to store when json is nesting another objects or arrays.Is there any way to accomplish this task
JSON is just a string. It could be stored in a TEXT or BLOB column.
The issue is that it is a structured string and it is tempting to reach into it to inspect/modify fields. Don't. It won't be efficient.
Copy values out of the JSON that you need for WHERE or ORDER BY (etc) so that MySQL/MariaDB can efficiently access them. Sure, keep copies inside the JSON text/blob, if you like.
Please describe your use for JSON, there may be more tips on "proper" usage inside a database.
That being said, MariaDB 10.2 has picked up much of Oracle's JSON. But there is not a JSON datatype. And there are a few function differences.
And this does not explain how far behind the 3rd party software (spring-boot, etc) is.
I am trying to figure out (and so far it seems I am out of luck), if it possible to refer (for association, for example) to a nested select from a different mapper/datasource ? Or may be some HashMap, which I could prepare manually beforehand..
What am I trying to achieve is basically the following:
say we have a database ONE, which has a table with a list of items. Every item has a field like "external_id"
we have also a database TWO, which is on different host and/or of a different driver type, which has a table with id; name pairs
And I would like to return a Mapped type which would contain fields from BOTH databases ONE and TWO.
Any ideas? So far I can only suggest constructing 2 POJOs independently, one with a returned ResultSet from db ONE and another with a list of id/name-pairs from db TWO and building the resulting object "by hands". But this is so boring...
I'm completely new to MongoDB and looking at moving my base persistence code (for many projects) over to it using JDO as an agnostic layer. So I'm asking this question from the perspective of a java developer who likes to the work with beans as the basic model unit.
My question is about subdocuments and whether they exists independently or are internally consolidated by MongoDB. i.e. if I had a domain structure like this:
Household - collection of Persons
Person
- name
- address
Address
- street
- postcode
If I had a document for a household it would have multiple Persons but each Person would have the same address.
Would each address be a distinct and separate entity within MongoDB (even though they are the same 'class' and have the same values. Or does Mongo somehow identify that they are referring to the same entity and internally store a UID for each Address?
More importantly. If I update the postcode for one address does that mean that every member of Household's address subdocument would reflect that change?
It seems if it does then it's straying into the relational sphere but without such referencing I can see horrible inefficiencies arising?
Mongo will not deduplicate those subdocuments for you, no. If you want to normalize that data, you'll need to save those addresses in to a different collection (ideally) and store DBRefs to those documents when you save the enclosing documents. Using something like morphia or spring-data can help manage those references for you.
If persisting data via JDO you have the choice of embedding the Person+Address into Household, or persisting as individual objects (just like you do with RDBMS). If storing as not-embedded then its up to you whether you have multiple copies of the same Person, or a single one referred to by multiple Households. If storing as embedded then they are embedded, so part of Household, hence info is dupd.
I want to read items from SQL table which are in Hierarchical manner. e.g. My table having Parent-Child relations.
Following is my table structure:
id name parentId
1 abc null
2 xyz 1
3 lmn 1
4 qwe null
5 asd 4
So I want to load all the Item hierarchically in one go, How can I do that?
I tried HashMap but it feels very complex....
thanks in advance...
You can retrieve the whole hierarchy through one SQL query, but the query depends on the database system - for instance, Oracle supports a "CONNECT BY" query for hierarchical queries. MySQL does not support this directly - but there are some very good approaches explained at Hierachical Queries in MySQL. I suggest that you try one of these. The basic approach is to create a function which imitates the CONNECT BY.
First of all, form your question it is not clear if you need help on SQL side or java data structure side.
I assume both i.e. you need to fetch data from database & then populate in appropriate java data structure.
So there is no direct way for this. Your data is mainly TREE STRUCTURE, so mainly you need to focus on java data structure for tree. So below are the steps that you need to take.
1) First create java class structure for tree structure. Refer below links for that.
Java tree data-structure?
http://www.java2s.com/Code/Java/Collections-Data-Structure/Yourowntreewithgenericuserobject.htm
http://www.codeproject.com/Articles/14799/Populating-a-TreeView-Control-from-the-Database
2) Then you don't need much from sql side if you finally need java objects. SO you can do plain select query to fetch data.
3) Now you need to have a helper method for preparing tree for you data using class structure form step 1. If you do better design, you tree class structure can be made self constructing using SQL data.
I'm working on a project that save/retrieve JSON-type string to a database.
Everything works ok, i.e., save and update is safe for types String,Number and Boolean, but for List and Map, I want to see what is the safe way to manipulate List and Map when the data comes back and forth the database especially when items on the list become large, i.e thousands of items, say for list of "friends" and "followers"
I am also concerned with potential data corruption when processing the a JSON List or Map in Java.
What is the safe way to update a List and Map using JSON.Simple library while not loading everything (every item) in memory.
For example I just need to insert one (1) item in a JSON list string that is stored in a database.
JSON isn't suitable for ORM (object relational mapping). This is why NoSQL databases store JSON as document (i.e. the whole thing). Therefore, JSON.Simple has no support for lazy loading part of the JSON structures.
Relational database don't map well to JSON (except for primitives) as you noticed because the natural data structure for List is a 1:N mapping where the list type has an index column (i.e. position of the element in the list) while Map needs an N:M mapping.
So a better solution might be to store the whole JSON string in a CLOB (instead of trying to break it apart) or saving the whole JSON string in a CLOB + extracting a few key fields so you can index the data properly.
But when you work with the JSON you will either have to write your own OR mapper which supports lazy loading of JSON arrays and maps or you will have to read the whole structure into RAM every time.