如何使用 Mongoose 将嵌入文档从一个文档放入另一个文档?
我想做的事情应该是直接的,但由于某种原因,我很难弄清楚这一点。我有以下猫鼬模式(简化)。
var Status = new Schema({
name : { type: String, required: true },
description : { type: String }
});
var Category = new Schema({
statuses : [Status], // contains a list of all available statuses
// some other attributes
});
var Book = new Schema({
statuses : [Status], // preferably this would not be an array but a single document, but Mongoose doesn't seem to support that
// some other attributes
});
现在,我想要执行以下操作:
- 检索类别文档
- 查找特定的嵌入式状态文档(基于请求参数)
- 将该特定的嵌入式状态文档分配给特定的书籍文档。我想替换现有的图书状态,因为在任何给定时间都应该只为一本书设置一个状态。
这是我目前正在做的事情:
mongoose.model('Category').findOne({_id: id}, function(err, category){
if(err) next(err);
var status = category.statuses.id(statusId); // statusId available via closure
book.statuses[0] = status; // book available via closure; trying to replace the existing status here.
book.save(function(err){
if(err) next(err);
next();
});
});
上面的代码似乎运行良好,并且没有出现任何错误。但是,新状态不会保存到文档中。下次输出更新后的 Book 文档时,它仍将具有旧状态。我调试了这个, find() 方法以及设置状态似乎都很好。
我现在唯一能想到的是,不知何故,我分配的状态值不是用 Mongoose 保存的正确格式。尽管如此,我预计会出现某种错误消息。
或者也许有更好的方法来完成这一切?
What I'm trying to do should be straight forward but for some reason I'm having real difficulties figuring this out. I have the following Mongoose schemas (simplified).
var Status = new Schema({
name : { type: String, required: true },
description : { type: String }
});
var Category = new Schema({
statuses : [Status], // contains a list of all available statuses
// some other attributes
});
var Book = new Schema({
statuses : [Status], // preferably this would not be an array but a single document, but Mongoose doesn't seem to support that
// some other attributes
});
Now, I want to do the following:
- Retrieve the Category document
- Find a particular embedded Status document (based on request param)
- Assign that particular embedded Status document to a particular Book document. I want to replace the existing Book status as at any given time there should only be one status set for a book.
Here is what I'm currently doing:
mongoose.model('Category').findOne({_id: id}, function(err, category){
if(err) next(err);
var status = category.statuses.id(statusId); // statusId available via closure
book.statuses[0] = status; // book available via closure; trying to replace the existing status here.
book.save(function(err){
if(err) next(err);
next();
});
});
The above seems to run fine and I don't get any errors. However, the new status is not saved to the document. Next time I output the updated Book document, it will still have the old status. I debugged this and the find() methods as well as setting the status seems to be fine.
The only thing I can think of right now is that somehow the status value I'm assigning is not in the right format to be saved with Mongoose. Although, I would expect some kind of error message then.
Or maybe there is a better way to do all of this anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为您正在尝试复制嵌入文档,该文档本身可能有一个与之关联的 ObjectId。尝试在
Book
中保存重复的Status
将会创建两个具有相同ObjectId
的嵌入文档。尝试创建一个新的 Status 对象并复制字段。很难在
ObjectsId
上找到嵌入文档的文档,但这里提到了它们:http://mongoosejs.com/docs/embedded-documents.html。It could be because you are trying to copy an embedded document, which itself could have an ObjectId associated with it. Trying to save the duplicate
Status
within theBook
would create two embedded documents with the sameObjectId
. Try creating a new Status object and copying the fields.It is hard to find docs on
ObjectsId
s for embedded documents, but they are mentioned here: http://mongoosejs.com/docs/embedded-documents.html.