如何使用 Mongoose 将嵌入文档从一个文档放入另一个文档?

发布于 2024-11-09 19:07:25 字数 1271 浏览 0 评论 0原文

我想做的事情应该是直接的,但由于某种原因,我很难弄清楚这一点。我有以下猫鼬模式(简化)。

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
});

现在,我想要执行以下操作:

  1. 检索类别文档
  2. 查找特定的嵌入式状态文档(基于请求参数)
  3. 将该特定的嵌入式状态文档分配给特定的书籍文档。我想替换现有的图书状态,因为在任何给定时间都应该只为一本书设置一个状态。

这是我目前正在做的事情:

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:

  1. Retrieve the Category document
  2. Find a particular embedded Status document (based on request param)
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-11-16 19:07:25

这可能是因为您正在尝试复制嵌入文档,该文档本身可能有一个与之关联的 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 the Book would create two embedded documents with the same ObjectId. Try creating a new Status object and copying the fields.

It is hard to find docs on ObjectsIds for embedded documents, but they are mentioned here: http://mongoosejs.com/docs/embedded-documents.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文