Search code examples
node.jsmongodbmongoskin

How to use a list of objects inside an object of MongoDB using Mongo-Skin?


I'm using mongoDB with mongoskin on top of Node.JS.

I have a list of images (collection 'images') and for each image I'd like to save a list of comments.

I believe the right way with mongodb is to use a list of comments inside of each Image document.

The problem - I have no idea how to do this. How do I use lists inside of documents? and how do I perform CRUD on them?

Many thanks for the help.


Solution

  • this is how you would do it with the driver (mongoskin is just a thin layer on top)

    var id = new ObjectId();
    var image = {_id: id, title:"some title".......}
    collection.insert(image, {safe:true}, function(err, result) {
      var comment = {title:'comment'}
    
      collection.update({_id:id}, {$push:{comments:comment}}, {safe:true}, function(err, nrofUpdated) {
      }
    })
    

    useful links

    http://docs.mongodb.org/manual/

    http://mongodb.github.com/node-mongodb-native/