Pergunta

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.

Foi útil?

Solução

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/

Outras dicas

There is no direct way to do the traditional CRUD on embedded objects. You can do Create and READ easily, but Update and Delete, and very rudimentary. You cannot select just a single embedded object; you can only select the parent object(the one that has _id). A list of embedded objects is an array with objects are entries.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top