-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
am using mongoose 3.8.8 and I have been trying to update a subdocument and haven't made progress #2686
Comments
updating a comment does not persist to the database. |
Where is this issue actually happening? Is the |
The save() call not saving the update. { |
But when we do a find() on the same pin we have |
Do you get an error in the |
I don't get an error in the save() callback. The object that gets returned in the call back shows that it was saved but when the document is pulled again the change doesn't persist. This is the object that get returned in the callback { |
while this is the object that remains unchanged after the save when the document is pulled from the database { |
notice that the text field did not change above as compared to the what we have in the response we get from save() callback |
What are you using to pull the data from the database afterward? Can you show me the mongo shell output? |
we are using mongoLab . We usually use the mongo lab GUI |
Try doing
|
This is the output of running -: mongoose.set('debug', true) |
hmmm looks like your save operation is getting translated to an // Put this log statement to output the internal state of the doc before save
console.log(require('util').inspect(req.pin.$__));
// Save code
req.pin.save(function(err, pin){
// ...
}); |
any update on this issue ? |
Nope. Are you having the same issue? |
No update for a while. Re-open if still an issue. |
Any update on this issue? I´m experiencing exactly the same one. |
Provide code sample please. |
Of course.
This is the schema itself:
I´ve installed mongoose via npm ("mongoose": "4.x.x"). |
@iMarvinS Hmm the below script works as expected for me: var assert = require('assert');
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017/gh2686');
var Schema = mongoose.Schema;
var userSchema = new Schema({
name: String,
mail: String,
passwd: String,
state: { type: Object, default: {
isHome: false,
updated: new Date()
}}
});
var User = mongoose.model('User', userSchema);
User.create({}, function(error, user) {
assert.ifError(error);
user.state.isHome = true;
user.state.modified = new Date();
user.save(function(err, updated) {
User.findOne({ _id: updated._id }, function(error, user) {
console.log(JSON.stringify(user));
process.exit(0);
});
});
}); Output:
Can you clarify what sort of unexpected behavior you're seeing? "Doesn't work" is usually not sufficiently clear. |
Your example seems to work fine but check out this one:
If I want to find a specific user, modify its properties and then save the user object to mongodb it simply doesn´t save the modified properties to the server. |
I just found out about the doc.markModified() method.
Now it works like expected. From my side this topic can be closed now. |
Ah you're right, that's slightly different. You have to call |
Looking a bit more closely at this, you need to call Reason why my example worked was that the doc was when I modified |
pin schema 👍
var PinSchema = new Schema({
name: {
type: String,
trim: true,
required: 'Pin must have a name'
},
url: String,
image: {
type: String,
required: 'Pin must have an Image'
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
board: {
type: Schema.ObjectId,
ref: 'Board'
},
likes: [LikesSchema],
comments: [CommentSchema],
created: {
type: Date,
default: Date.now
}
});
exports.update = function(req, res) {
_.forEach( req.pin.comments, function(comment, key) {
if (req.params.commentId.toString() === comment._id.toString()) {
comment.text = req.body.text;
}
});
//we have a problem : update on comment does not persist to the doc in db.
console.log('newPin', req.pin.comments);
req.pin.save(function(err, pin) {
if (err) {
return res.status(400).send({
message: 'Could not update a comment'
});
} else {
res.json(pin);
}
});
};
The text was updated successfully, but these errors were encountered: