【问题标题】:Meteor Simple-Schema Collection2 help needed for build nested schema构建嵌套模式所需的 Meteor Simple-Schema Collection2 帮助
【发布时间】:2014-05-09 02:59:36
【问题描述】:

我正在尝试使用 Meteor Collection2 构建一个集合模式。

我的收藏的可能架构是:

{
 items: {
     id: Meteor.ObjectID
     title: String,
     Desc: String,
     createdAt: new Date(),
     creator: String,
     invitedUsers: [String],
     items2: [{
        id: String,
        pos: Number,
        dur: Number,
        startTime: Number,
        endTime: Number,
        duration: Number
        items3: [{
                    id: String,
                    itemtype: String,
                    style: String,
                    items4: [{
                            id: String,
                            position: Number,
                            dome: String
                            }]
                    }]

        }]
   }
}

那么我怎样才能最好地使用上述嵌套模式构建 Collection2 集合,以及对其执行插入、更新和删除查询的最佳方式。

更新:

所以现在正如 Andrei Karpushonak 所建议的,这就是我所得到的:

Item4 = new SimpleSchema({
    position: {
        type: Number
    },
    dome: {
        type: String
    }
});
    Item3 = new SimpleSchema({
    itemtype: {
        type: String
    },
    style: {
        type: String
    },
    items4: {
        type: [Item4]
    }
});

Item2 = new SimpleSchema({
    pos: {
        type: Number
    },
    dur: {
        type: Number
    },
    startTime: {
        type: Number
    },
    endTime: {
        type: Number
    },
    duration: {
        type: Number
    },
    items3 : {
        type: [Item3]
    }
});


Items = new Meteor.Collection2('items', {
    schema : new SimpleSchema({
        title: {
            type: String
        },
        Desc: {
            type: String
        },
        createdAt: {
            type: new Date()
        },
        creator: {
            type: String
        },
        invitedUsers: {
            type: [String]
        },
        items2: {
            type: [Item2]
        }
    })
});

所以现在我想弄清楚如何在这样的架构上执行插入、更新、删除操作? 我是否为整体的单个模式做?举个例子会很有帮助。

我们将不胜感激。

提前致谢,

普拉尼

【问题讨论】:

    标签: mongodb collections meteor meteor-collection2


    【解决方案1】:

    你有两个选择:

    创建子模式:

    item2 = new SimpleSchema({
      id: String,
      pos: Number
    })
    
    item1 = new SimpleSchema({
      id: Meteor.ObjectID,
      title: String,
      items2: [item2]
    });
    

    使用点符号:

    item1 = new SimpleSchema({
      id: String,
      pos: Number,
      "item2.id": String,
      "item2.pos": String
    });
    

    我认为第一种方法更适合您的模型,因为您将对象数组作为 items2

    的值

    【讨论】:

    • 谢谢伙计,刚刚更新了可能的架构和集合代码。现在我想弄清楚如何对其执行插入、更新、删除操作。有任何想法吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多