Frage

I am storing some kind of filesystem in Mongo, where directories are named categories.

The category JSON looks like:

{
   "name":"CategoryChildLevel2",
   "parentId":"2",
   "otherAttribute":"anyVal",
   "breadcrumb":[
      {
         "name":"RootCategory",
         "id":"1"
      },
      {
         "name":"CategoryChildLevel1",
         "id":"2"
      }
   ]
}

The FS categories are linked together with the parentId attribute.

I need to display the categories breadcrumbs. Through user navigation, we can know where we are on the FS, but categories can be accessed directly by their ID (bookmarked category, search engine...), without any FS navigation. To avoid recursive calls to the DB, to be able to get the breadcrumb, I have denormalized it.


The problem is that this breadcrumb is hard to keep up to date, because a top level category can be moved and thus all its childs breadcrumbs must be updated. There can be many child categories to update, and there are different ways to deal with this problem. Some of them are secure but expensive (recursion), and others are faster but could lead to some inconsistencies.


Here what I'd like to know is if it is possible to do a query to retrieve categories that have a bad breadcrumb. I need a query that permits to do:

Retrieve all the categories that do not have: last array element breadcrumb.id = parentId

I don't think the "last array element" part is possible, but it would also be nice to be able to do:

Retrieve all the categories that do not have: breadcrumb.id contains parentId

Any solution, available in Scala or Java driver? I'm using Salat/Casbah.

This question may help you to understand what I'm facing: Which DB would you use? MongoDB/Neo4j/SQL... all of them?

War es hilfreich?

Lösung

You can do the Retrieve all the categories that do not have: last array element breadcrumb.id = parentId query with a $where operator:

db.test.find({
    // Find docs were breadcrumb is empty or its last element's id != parentId
    $where: '!this.breadcrumb.length || this.breadcrumb[this.breadcrumb.length-1].id !== this.parentId' 
})
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top