i have data structure (classes comments) , want add 1-level deep reply. means i'd add object on "comentarios" element.
how can achieve mongo? means: match cursada (db) id, match clase (first array) id, match comment (second array) id, add new element there.
db.cursada.find({"_id": objectid("55444f56e5e154f7638b456a")}).pretty() { "_id" : objectid("55444f56e5e154f7638b456a"), "clases" : [ { "_id" : "554e7f2fe5e154797d8b4578", "titulo" : "qewewqewq" }, { "_id" : "554e8be0e5e154dc698b4582", "titulo" : "la mejor clase" }, { "_id" : "554eb90de5e154dd698b458b", "comentarios" : [ { "_id" : objectid("55a021afe5e154cf098b4567"), "nombreusuario" : "nombre", "texto" : "432432423" } ], "titulo" : "peeling - cosa" }, { "_id" : "554e91a0e5e154797d8b4587", "titulo" : "fdsfdsa" }, { "_id" : "554f8f50e5e154dd698b458f", "titulo" : "clase2" }, { "_id" : "554f99dae5e154797d8b45a7", "titulo" : "con profesor" }, { "_id" : "554fa4a0e5e154797d8b45c4", "titulo" : "profesor nombre nombre" }, { "_id" : "5557b37be5e154e07f8b4567", "titulo" : "dermatologia i" }, { "_id" : "5557c701e5e154066d8b456c", "titulo" : "acido hialuronico" } ], "curso" : "552fa5f1e5e1542e628b4567", "fechafin" : "2015-05-22t03:00:00.000z", "fechaini" : "2015-05-08t03:00:00.000z", "titulo" : "cremotas" } getting result:
{ "_id" : objectid("55444f56e5e154f7638b456a"), "clases" : [ { "_id" : objectid("554eb90de5e154dd698b458b"), "comentarios" : [ { "_id" : objectid("55a021afe5e154cf098b4567"), "nombreusuario" : "nombre", "texto" : "432432423", ----------------here "replies": [ { "_id": ...., "user": ...., "text":...., "date":.... }] ----------------here } ], "titulo" : "peeling - cosa" }, ] }
one twisted example!
luckily presents no problem mongodb , $elemmatch query operator:
model.update({ _id: "55444f56e5e154f7638b456a", classes: { $elemmatch: { _id: "554eb90de5e154dd698b458b", } } }, { $push: { 'classes.$.comentarios': { nombreusuario: 'new comment nombreusuario', texto: '111242515' } } }, function(err, count) { console.log('updated ' + count + ' document'); }); what happens here?
first, we're specyfing specific course ("cursada") update:
_id: "55444f56e5e154f7638b456a" then, using $elemmatch operator, we're restricting result of query contain 1 class:
classes: { $elemmatch: { _id: "554eb90de5e154dd698b458b", } } now, having found specific class update, can add new comment it. here's our update query:
$push: { 'classes.$.comentarios': { nombreusuario: 'new comment nombreusuario', texto: '111242515' } } $pushoperator tells mongodb add new comment specified array of comments.'classes.$.comentarios'specifies nestedcomentariosarray update, using$positional operator.- finally, , part should self-explanatory: full object of new comment add specified class.
it's worth mentioning if you're running such nested structure, perhaps it's worth thinking spreading courses, classes, possibly comments on separate mongodb collections.
when nested documents way go , when it's better create separate collections can tricky question answer - here's nice presentation discussing issue.
Comments
Post a Comment