How to Handle Complex Tagging in RavenDB
Join the DZone community and get the full member experience.Join For Free
in the ravendb mailing list, we got a question about tagging. in this case, the application need:
1. tags have identity ("set" has a different meaning if i'm talking math, music or sports).
2. i want to know who tagged what and when.
3. i want to do this once, as a service, so i don’t need have ids in each document i want to tag. in my app, there are many such document types.
let us see how we can approach this in ravendb. we are going to do it like so:
note that because tags have identity, we store only the tag id inside the tagged object, along with the required information about who & when it was tagged.
now, let us try to have some fun with this. let us say that i want to be able to show, given a specific album, all the albums that have any of the same tags as the specified album.
we start by defining the following index:
note that the naming convention matches what we would expect using the default linq convention, so we can easily query this index using linq.
and now we want to query it, which, assuming that we are starting with albums/1, will look like:
this translate to “show me all of the albums that share any of the specified tags, except albums/1”.
and this is pretty much it, to be fair. oh, if you want to show the tags names you’ll have to include the actual tags documents, but there really isn’t anything complex going on.
but what about the 3rd requirement?
well, it isn’t really meaningful. you can move this tags collection to a the layer super type, but if you want to be able to do nice tagging with ravendb, this is probably the easiest way to go.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.