将深JSON对象转换为Mongo架构

时间:2016-12-28 22:44:11

标签: json mongodb meteor

我正在尝试将一个深JSON对象提交到Mongo Collection模式中。该对象具有一个有趣的形状:

{
  data: {
    a: {
      'kjndsgheid': { foo: true, bar: false },
      'weidmeirfi': { foo: true, bar: false },
      'dfnbewetee': { foo: true, bar: false },
      'dbnfryhred': { foo: true, bar: false }
    },
    b: 1
  }
}

这让我感到困惑,因为a是一个对象,其键是随机ID - 我可以将a存储为blob,但是每个随机键的属性都是固定的,我希望这些属性在架构。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

我试试这个,虽然它涉及稍微修改架构:

ASchema = new SimpleSchema({
  key : {
    type : String,
  },
  foo : {
    type: Boolean,
  },
  bar : {
    type : Boolean,
  }

})

DataSchema = new SimpleSchema({
  a : {
    type : [ASchema],
  },
  b : {
    type: Number,
  },

})

Data = new Mongo.Collection('data')
Data.attachSchema(DataSchema)

插入将涉及在旅途中修改您的JSON。或者,如果您不关心验证密钥,则可能需要使用对象类型而不是自定义ASchema。