[英]Use Mongoose to update subdocument in array
There are a few posts about this ( here and here , for example), but none of them use a native Mongoose method for this. 有一些关于此的帖子(例如这里和这里 ),但没有一个使用本地Mongoose方法。 (The first one uses
$set
, the second one uses the extend
npm package. Seems like there should be a "native mongoose" way to do this though. (第一个使用
$set
,第二个使用extend
npm包。看起来应该有一个“本地猫鼬”方式来做到这一点。
var blogPostSchema = new mongoose.Schema({
title: String,
comments: [{
body: String
}]
});
Here's what I tried originally: 这是我最初尝试的内容:
BlogPost.findById(req.params.postId, function (err, post) {
var subDoc = post.comments.id(req.params.commentId);
subDoc = req.body;
post.save(function (err) {
if (err) return res.status(500).send(err);
res.send(post);
});
});
Problem is this line: subDoc = req.body
isn't actually changing the parent doc's subDoc, but rather is just passed by reference. 问题是这一行:
subDoc = req.body
实际上并没有改变父文档的subDoc,而只是通过引用传递。 Nothing actually changes in the database after a save()
call. 在
save()
调用之后,数据库中实际上没有任何变化。
The extend
package fixes this by merging the two objects together, as shown in the second SO post linked above (and again here ). extend
包通过将两个对象合并在一起来修复此问题,如上面链接的第二个SO帖子( 此处再次)所示。 But isn't there a way to get around this using some Mongoose API method? 但是,有没有办法使用一些Mongoose API方法解决这个问题? I can't find anything in the documentation about it (only how to add new subdocs and remove subdocs).
我在文档中找不到任何关于它的东西(只有如何添加新的subdocs和删除subdocs)。
An alternative I wrote was this: 我写的另一个选择是:
for (var key in subDoc) {
subDoc[key] = req.body[key] || subDoc[key];
}
which also works, but I wasn't sure if there was anything dangerous about doing it this way. 这也有效,但我不确定这样做是否有任何危险。 Maybe this is the best way?
也许这是最好的方法?
Thanks in advance! 提前致谢!
The reason subDoc = req.body
doesn't work is that it is replacing the reference that was created by the first assignment to subDoc
with a brand new reference to req.body
(assuming it is an object and not a string). 其原因
subDoc = req.body
不起作用是,它是替换,其通过所述第一分配创建到参考subDoc
一个全新的参照req.body
(假设它是一个对象,而不是一个字符串)。 This has nothing to do with Mongoose but just how references work in general. 这与Mongoose无关,而是一般的参考工作方式。
var objA = { a: 1, b: 2}; var objB = { a: 'A', b: 'B'}; var objC = objA; console.log('Assigning ref to objC from objA', {objA, objB, objC}); // It should console.log the following: // Assigning ref to objC from objA { // "objA": { // /**id:2**/ // "a": 1, // "b": 2 // }, // "objB": { // "a": "A", // "b": "B" // }, // "objC": /**ref:2**/ // } // objC = objB; console.log('Replacing ref to objC with objB', {objA, objB, objC}); // It should console.log the following: // Replacing ref to objC with objB { // "objA": { // "a": 1, // "b": 2 // }, // "objB": { // /**id:3**/ // "a": "A", // "b": "B" // }, // "objC": /**ref:3**/ // }
You can use the Document.#set
method . 您可以使用
Document.#set
方法 。 Just provide it an object (in this case req.body
) that mirrors the document's structure with the values you want to set. 只需为它提供一个对象(在本例中为
req.body
),该对象使用您要设置的值镜像文档的结构。
BlogPost.findById(req.params.postId, function(err, post) {
var subDoc = post.comments.id(req.params.commentId);
subDoc.set(req.body);
// Using a promise rather than a callback
post.save().then(function(savedPost) {
res.send(savedPost);
}).catch(function(err) {
res.status(500).send(err);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.