node.js - 通过 Mongoose 在 MongoDB 中存储嵌入另一个文档的文档副本

标签 node.js mongodb mongoose

我们需要存储一个 Mongo 文档的副本,作为另一个文档中的嵌入子文档。它应该有对原始文档的引用。复制的文档需要是深拷贝,就像原始文档的快照一样。

原始文档的架构(用 Mongoose 定义)不固定 - 它目前使用一种继承来允许根据“类型”对模式进行不同的添加。


  1. 有没有办法在 Mongoose 模型中实现如此灵活的嵌入式模式?
  2. 它是不是需要在运行时注入(inject),什么时候我们可以知道 架构?

我们目前的模型/模式是这样的:

///UserList Schema: - this should contain a deep copy of a List
user: {
    type: ObjectId,
    ref: 'User'
},
list: {
    /* Not sure if this is a how we should store the reference 
    type: ObjectId,  
    ref: 'List'
     */
    listId: ObjectId,
    name: {
        type: String,
        required: true
    },
    items: [{
        type: ObjectId,
        ref: 'Item'
    }]
}

///List Schema:

name: {
    type: String,
    required: true
},
items: [{
    type: ObjectId,
    ref: 'Item'
}],
createdBy: {
    type: ObjectId,
    ref: 'User'
}   

我们目前的代码使用继承来允许不同的项目类型。我意识到这种技术可能不是实现我们所需灵 active 的最佳方式,也不是我问题的重点。

///Item Model + Schema
var mongoose = require('mongoose'),
nodeutils = require('util'),
Schema = mongoose.Schema,
ObjectId = Schema.Types.ObjectId;

function ItemSchema() {
    var self = this;
    Schema.apply(this, arguments);

    self.add({
        question: {
            type: String,
            required: true
        }
    });

    self.methods.toDiscriminator = function(type) {
        var Item = mongoose.model('Item');
        this.__proto__ = new Item.discriminators[type](this);
        return this;
    };
}

nodeutils.inherits(ItemSchema, Schema);
module.exports = ItemSchema;

最佳答案

我认为您只需要在父 mongoose 模式中为文档创建一个空的 {} 对象。通过这种方式,您将能够存储任何对象及其所有数据的硬拷贝。

parentobj : {
    name: Sring,
    nestedObj: {}
}

我认为此时,您需要做的是在保存之前将嵌套对象标记为已修改。这是我的 Mongoose 代码示例。

exports.update = function(req, res) {
  User.findById(req.params.id, function (err, eluser) {
    if (err) { return handleError(res, err); }
    if(!eluser) { return res.send(404); }
    var updated = _.merge(eluser, req.body);
    //This makes NESTEDDATA  OBJECT to be saved
    updated.markModified('nestedData');
    updated.save(function (err) {
      if (err) { return handleError(res, err); }
      return res.json(200, eluser);
    });
  });
};

另外,如果你需要nestedDocument中不同文档的数组,正确的做法是这样:

parentobj : {
    name: Sring,
    nestedObjs: [Schema.Types.Mixed]
}

请查看Mongoose Schema Types仔细

编辑

如您所说,我将向您添加最终解决方案,将 ItemSchema 包含在 nestedObj 数组定义中,以将对象的类型明确为确定的类型。

var ItemSchema = new Schema({
    item1: String,
    item2: String
});

var parentobj = new Schema({
    name: Sring,
    nestedObj: [ItemSchema]
});

编辑 2: 记住向 nestedArray 添加新项目,必须使用 nestedArray.push(item) 完成

问候!!

关于node.js - 通过 Mongoose 在 MongoDB 中存储嵌入另一个文档的文档副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29626942/

相关文章:

node.js - 在 nodejs 中使用 expressjs、mongoosejs 和 redis 在哪里编写业务逻辑

node.js - 使用 Mongoose 库创建查询(查找数组中的对象)

node.js - 如何在nodejs中将原始内存复制到Buffer?

node.js - Meteor.js 在没有日志的情况下被杀死

黑板模式控制流程中的Javascript回调函数

MongoDB:聚合特定子文档的所有字段的值

javascript - Node JS - 如何使用 URL 查询字符串?

mongodb - 如何使用 Mongodb 进行聚合分组并显示其他字段?

mongodb - 选择一个包含元素数量有限的数组的文档

mongodb - 获取给定用户每个对话的未读消息计数