MongoDb

NoSQL Data Store from TenGen

http://www.mongodb.org

Feb'2013: Fault Tolerance still broken?

schema design

Sept27'2010 - just using MongoDb Command Line before moving to Django And Mongodb

  • create db use mf

  • > mf.master_projects.save({title: 'Make a Will for each Adult', priority: 1, reasoning: 'Why? Because I said so.'}) - get _id:'4ca1397c063911217433201c'

  • now how embed a first Task inside this? > mf.master_projects.update({_id:'4ca1397c063911217433201c'}, {$push{children: [{title: 'Decide on process and resources'}]}})

    • get Syntax Error: missing : after property id (shell):0

    • try embedding approach{{{ var mp1 = db.master_projects.findOne({_id:[Object Id]}); mp1.children = [{title: 'Decide on process and resources'}]; db.master_projects.save(mp1); }}}

      • yes, that worked.
    • try first approach again (on a 2nd subtask), because realize that wasn't using Object Id() function before: mf.master_projects.update({_id:Object Id('4ca1397c063911217433201c')}, {$push{children: [{title: 'Make guardianship/trustee decisions'}]}}) - nope, still get same Syntax Error: missing : after property id

    • Trying simpler update... hmm, maybe confusing when to use db. vs mf. {{{

mf.master_projects.update({_id:[Object Id]}, {$inc: {priority: 1}}) Tue Sep 28 15:30:45 Reference Error: mf is not defined (shell):0 db.master_projects.update({_id:[Object Id]}, {$inc: {priority: 1}}) db.master_projects.find() { "_id" : [Object Id], "title" : "Make a Will for each Adult", "priority" : 2, "reasoning" : "Why? Because I said so.", "children" : [ { "title" : "Decide on process and resources" } ] } }}}

  • but not helping on embedded/child case: {{{

db.master_projects.update({_id: [Object Id]}, {$push{children: {title: 'Make guardianship/trustee decisions'}} }) Tue Sep 28 15:35:55 Syntax Error: missing : after property id (shell):0 }}}

  • duh, found problem, I was missing : after $push - so this is good: {{{ db.master_projects.update({_id: [Object Id]}, {$push: {children: {title: 'Make guardianship/trustee decisions'}} }) }}}

Sept29

  • add child #4 (note we skipped #3) {{{

db.master_projects.update({_id: [Object Id]}, {$push: {children: {title: 'Make Living Will decision'}} }) }}}

  • add child #3 (between 2 and 4) - thought I'd seen an example somewhere, but now I can't find it. Going to try the equivalent of the multi-step findOne()/save() approach used above:{{{

mp1.children.insert(2, {title: 'Make asset dispersal decisions'}) Wed Sep 29 10:00:45 Type Error: mp1.children.insert is not a function (shell):0 }}}

* [posted](http://groups.google.com/group/django-users/browse_thread/thread/db5c92bce85eb11f) to group
  • have found no functionality to insert into the middle of a list of embedded documents. Therefore will add a sort_order field to each subtask. (The reason I was avoid this is that it means that inserting into the middle of the list requires rewriting the sort_order for all peers that should come after the new node (or else allowing decimal values for sort_order, which I'm not crazy about...).)

    • so, to do that..... remove all data and start over {{{

db.master_projects.remove({}) db.master_projects.save({title: 'Make a Will for each Adult', priority: 1, reasoning: 'Why? Because I said so.'}) db.master_projects.find() { "_id" : [Object Id], "title" : "Make a Will for each Adult", "priority" : 1, "reasoning" : "Why? Because I said so." } db.master_projects.update({_id: [Object Id]}, {$push: {children: {title: 'Decide on process and resources', sort_order: 1}} }) }}}

Oct01

  • mental flip: tweaking a sub-document just means that you've grabbed the whole document, tweaked the copy of it, then saved that whole-document-copy back, right? So going to use PyMongo, so all that bit in the middle involves standard Python dictionary games.

    • install PyMongo into my VirtualEnv

    • working, but can't find example doing find_one() query on id {{{

master_projects.find_one({id: [Object Id]}) Traceback (most recent call last): File "", line 1, in Name Error: name 'Object Id' is not defined master_projects.find_one({'_id': [Object Id]}) Traceback (most recent call last): File "", line 1, in Name Error: name 'Object Id' is not defined master_projects.find_one({u'_id': [Object Id]}) Traceback (most recent call last): File "", line 1, in Name Error: name 'Object Id' is not defined }}}

  • doh! I was using the old Object Id value! Now working with {{{ mp1 = master_projects.find_one({"_id": [Object Id]}) }}}

  • next: add sub-sub child (knowing list position) {{{

mp1['children'][0]['children'] = [{'sort_order': 1, 'title': 'Use software tool/service'}] master_projects.save(mp1) }}}

  • next: create separate resources collection, add some resources, then add resources._id values as children of tasks

    • nope, change my mind on the schema

      • a task/subtask can have a resource_list

      • so we have a resource_lists collection

      • then a given resource can have a list of resource_lists values

      • reasons for this schema

        • can be adding to resource lists all the time without having multiple copies of lists all over the place...

        • hmm, thinking still...

    • in command-line: {{{

db.resource_lists.save({title: 'Will-making software'}) db.resource_lists.find() { "_id" : [Object Id], "title" : "Will-making software" } db.resources.save({title: "Nolo's Online Will", url: 'http://www.nolo.com/products/-NNWILL.html', resource_lists: [[Object Id]]}) }}}

* still have to assign that `resources_list` to `master_projects` sub-task.... - use PyMongo for that: {{{

mp1['children'][0]['children'] [{u'sort_order': 1, u'title': u'Use software tool/service'}] mp1['children'][0]['children'][0]['resource_list'] = [Object Id] db.master_projects.save(mp1) }}}


Edited:    |       |    Search Twitter for discussion