I started this blog to remind myself of the solutions to development issues I have along the way and as a general hinting mechanism to remind myself what is going on when I have been away from it for a while. If any of this means that someone else reading this saves themself a world of hurt or going down the rabbit-hole of death, then I'm glad that recording it publically rather than privately was helpful (though conversely, it is always important to learn from one's owner experiences).
In my first posting Simple mongodb equivalents, I wanted to remind myself of some of the basic differences around selection between SQL and NoSQL, and to give concrete examples of what this would look like in Java. In this posting I'll go through the next stage, the hinting mechanism for CRUD.
For those that associate crud with filth, flattery, disgust or disappointment, then you may be on the wrong blog. CRUD stands for CREATE, RETRIEVE, UPDATE, DELETE - though the RETRIEVE is usually replaced by get or SELECT at implemention and CREATE is replaced with INSERT. So, perhaps I'll call it ISUD instead.
First, set up the database, a user and set up an index.
The CREATE (or INSERT) of CRUD (or ISUD)
Here we a setting up some basic data in a collection named 'stuff' that lives in the 'isud' database.
...this is how you would do it from the mongo shell...
...and the Java implementation...
The RETRIEVE (or SELECT) of CRUD (or ISUD)
The selection or retrieval of a document from the collection 'stuff' can be rather powerful, as per Simple mongodb equivalents.
...this is how you would do it from the mongo shell...
...and the Java implementation...
Now let's do a similar thing but this time specify the fields we want returned.
...this is how you would do it from the mongo shell...
...and the Java implementation...
The UPDATE of CRUD
Everyone knows his name was Eric, not Derek, so let's fix that with an update.
...this is how you would do it from the mongo shell...
...and the Java implementation...
Please refer to Updating a document using the default ObjectId for the reasoning behind DBObjectFactory
The DELETE of CRUD
Finally, we can delete a document.
...this is how you would do it from the mongo shell...
...and the Java implementation...
It's all relatively straight forward really. Though none of this is rocket science, I for one find this a useful and succinct reminder or as previously stated; a hinting mechanism.
Showing posts with label ObjectId. Show all posts
Showing posts with label ObjectId. Show all posts
Tuesday, 3 January 2012
Thursday, 8 December 2011
Updating a document using the default ObjectId
Here are a few facts about keys and the way MongoDB deals with them.
An annoyance I had with MongoDB, when I first started to use it, was the explicit update required on the default _id key/value pair. To illusrate this here is a concrete example of what I mean.
I wanted to do this:
But I had to do this:
In this case, json is a JSON document passed from an HTML form via HTTP PUT, eventually ending up here at the DAO. I was using the default _id implementation and thus would have expected save to do the figure that out and do the necessary work for me.
I just can't live those extraneous lines of code, it's too messy and too much to type every time. I'll pull it out into a fromJson call, as I can see the pattern emerging and the odds of this reoccurring high.
I must have missed something in the API Docs as this cannot be an unusual requirement. I would of at least expect to see it in a Util class...
Factory snippet:
Usage snippet:
I get to do what I wanted to now, but it still feels wrong. It feels a little bit dirty. It feels like a hack-o-la. How can I do it better?
- Documents in MongoDB require a key, _id, which uniquely identifies them.
- This is a 12-byte binary value, read more from the source of truth, ObjectId.
- When inserting a new document, MongoDB will generate an ObjectId where one is not specified.
- There is a 'reasonable' chance that this will be unique at the time of creation.
- All of the officially-supported MongoDB drivers use this type by default for _id values.
An annoyance I had with MongoDB, when I first started to use it, was the explicit update required on the default _id key/value pair. To illusrate this here is a concrete example of what I mean.
I wanted to do this:
But I had to do this:
In this case, json is a JSON document passed from an HTML form via HTTP PUT, eventually ending up here at the DAO. I was using the default _id implementation and thus would have expected save to do the figure that out and do the necessary work for me.
I just can't live those extraneous lines of code, it's too messy and too much to type every time. I'll pull it out into a fromJson call, as I can see the pattern emerging and the odds of this reoccurring high.
I must have missed something in the API Docs as this cannot be an unusual requirement. I would of at least expect to see it in a Util class...
Factory snippet:
Usage snippet:
I get to do what I wanted to now, but it still feels wrong. It feels a little bit dirty. It feels like a hack-o-la. How can I do it better?
Subscribe to:
Posts (Atom)