문제

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.

도움이 되었습니까?

해결책

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/

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top