update an data in embedded document

569 views
Skip to first unread message

Oleg Slobodskoi

unread,
Feb 24, 2011, 5:11:21 PM2/24/11
to Mongoose Node.JS ORM
Hi

This is my schema:

var m = require('mongoose');

var A = new m.Schema({
name: {
type: String,
index: {
unique: true
}
},
data: {}
});

var B = new m.Schema({
title: String,
elements: [A]
});

var C = new m.Schema({
title: String,
elements: [B]
});

m.model('C', C);


This is an example of data, that can be saved there:


{ "b" : [
{
"elements" : [
{
"_id" : ObjectId("4d6648dbd633318116000003"),
"name" : "foo",
"data" : {
"foo" : "bar"
}
}
],
"_id" : ObjectId("4d6648dbd633318116000002"),
"title" : "foo"
}
], "_id" : ObjectId("4d6648dbd633318116000001") }


I want now to set b[0].elements[0].data.foo = '123456';



So here is my query:

var C = mongoose.model('C');


C.findById('c-id', function(err, c) {
var element = c.doc
.elements.id('b-id').doc
.elements.id('a-id');

element.doc.data.foo = '123456';
element.save(function(err) {
console.log(err); // null
});
});


There is no error and data is not updated.

Is the achema a bit complecated?


Thanks a lot for help.

Oleg






Luke Galea

unread,
Feb 24, 2011, 8:25:41 PM2/24/11
to Mongoose Node.JS ORM
Hi Oleg,

I've run into this too and logged it in github as an issue. For now, a
terrible work around is to remove the embedded object, save the
parent, reload the parent by finding it again, then adding the
modified embedded document and saving again.

I know that sounds crazy..

Siegfried Ehret

unread,
Feb 24, 2011, 9:05:08 PM2/24/11
to mongoo...@googlegroups.com, Oleg Slobodskoi
I haven't tried your schema but you can check here :
https://groups.google.com/d/topic/mongoose-orm/mP2Lh06kYts/discussion
I posted a working example with embedded documents in embedded documents.

Joe

unread,
Feb 25, 2011, 11:12:36 AM2/25/11
to Mongoose Node.JS ORM
Thanks for the example Siegfried.

I would get the same issue as well, mainly that after I updated an
embedded document and saved it, it would produce a duplicate embedded
document but with the updated values. In my case, something like this:

> db.fightmonsters.find()
{ "_id" : ObjectId("4d67d041e6a3b9d00c00000a"), "player" : [
{
"_id" : ObjectId("4d67d041e6a3b9d00c00000b"),
"name" : "Warrior",
"action" : "attack",
"hp" : 100,
"target" : ObjectId("4d67d03ae6a3b9d00c000001")
},
{
"_id" : ObjectId("4d67d041e6a3b9d00c00000c"),
"name" : "Monster 1",
"action" : "attack",
"hp" : 50,
"target" : ObjectId("4d5c35b0cdebc4780600000c")
},
{
"_id" : ObjectId("4d67d041e6a3b9d00c00000b"),
"name" : "Warrior",
"action" : "attack",
"hp" : 90,
"target" : ObjectId("4d67d03ae6a3b9d00c000001")
},
{
"_id" : ObjectId("4d67d041e6a3b9d00c00000c"),
"name" : "Monster 1",
"action" : "attack",
"hp" : 50,
"target" : ObjectId("4d5c35b0cdebc4780600000c")
}
], "status" : "Open", "user_id" :
ObjectId("4d571cfffa51b72407000005") }

However, following Siegfried's example, if at first you pull the saved
record from the database and work with that variable, instead of the
initial variable, then you do not get duplicate entries:

fightSession was the initial variable using "new", and session is the
variable with the pulled data:

fightSession = new FightMonster({
user_id:
req.curUser.id,
status: 'Open',
});

fightSession.player.push({
name: req.curChar.name,
action: req.params.action,
hp: req.curChar.stats.hp,
target: curMonster.id,
});

fightSession.player.push({
name: curMonster.name,
action: 'attack',
hp: curMonster.stats.hp,
target: req.curChar.id,
});
fightSession.save(function(err){
if(!err){

FightMonster.findOne({ status: 'Open' }, function (err,
session){
if(session){
session.player[0].hp += 20;
session.save(function(err){
console.log(session.toObject());
});
}
});

...

Siegfried Ehret

unread,
Feb 25, 2011, 9:31:07 PM2/25/11
to mongoo...@googlegroups.com, Joe
Hi guys,

I changed my little program.

I added a 'content' in the vote schema :
var Vote = new Schema({
  author:String,
  note:Number,
  content:{ type:String, default:'something' }
});

And in the view partials/comments/one.jade :
    - each vote in comment.votes
      li
        span= vote.note
        br
        span= vote.content

Finally, in my app.js, I update some stuff on the first comment :
post.comments.id(commentId).votes[0].content = 'update on : ' + new Date() + '\n' + vote.content;

Go to a post, play a little with the 'up' and 'down', and look at the date !

Hope this will help !
sumit_20110226_1127.rar

Oleg Slobodskoi

unread,
Feb 26, 2011, 5:27:25 AM2/26/11
to mongoo...@googlegroups.com
I couldn't get your program running, because of

500 TypeError: Object # has no method 'urlDecode'


it must be some incompatible module versions, you need a package.json.

but looking into your source:

post.comments.id(commentId).votes.push(vote);


post.comments.id(commentId).votes[0].content = 'update on : ' +
new Date() + '\n' + vote.content;

your are pushing a new entry to votes first.


Can you try please just to modify ONLY some content ?

Oleg


2011/2/26 Siegfried Ehret <tartifl...@gmail.com>:

--
regards,
Oleg @oleg008
github.com/kof

Siegfried Ehret

unread,
Feb 26, 2011, 6:41:25 AM2/26/11
to mongoo...@googlegroups.com
You'll need express, mongoose, and form2json.
I changed a little my program.
If no 'vote', it creates one vote (only one, it does not push another one).
Then, to show how it updates, I '+=' the content property with the date.
The content property is the only thing which is updated when you 'up' or 'down'.
Use these buttons to show the magic.
sumit_20110226_2040.rar

Oleg Slobodskoi

unread,
Feb 26, 2011, 6:54:52 AM2/26/11
to mongoo...@googlegroups.com
which versions of components do you use, I have installed the latest
of them and have the issue

2011/2/26 Siegfried Ehret <tartifl...@gmail.com>:

--
regards,
Oleg @oleg008
github.com/kof

Siegfried Ehret

unread,
Feb 26, 2011, 6:58:09 AM2/26/11
to mongoo...@googlegroups.com, Oleg Slobodskoi
express 1.0.7
mongoose 1.0.16 (i haven't updated to 1.1.0)
form2json, I don't know... 0.0.2 I think.

Siegfried Ehret

unread,
Feb 26, 2011, 6:58:19 AM2/26/11
to mongoo...@googlegroups.com, Oleg Slobodskoi
And node 0.2.6
Reply all
Reply to author
Forward
0 new messages