我正在尝试学习mongo/mongoose,并将json导入到MongoDB中。Mongoose - 将json对象映射到Schema
我的JSON包含了一些形式的对象:
{
"FIELD1": "28/02/2017",
"FIELD2": "string value",
"FIELD3": "100"
},
{
"FIELD1": "28/02/2017",
"FIELD2": "string",
"FIELD3": "57"
},
我有一个模式:
var statementSchema = new Schema({
//date: {type:Date, required: true},
//name : {type:String, required:true},
//amount: {type: Number, required:true}
FIELD1 : {type:String, required:true},
FIELD2: {type:String, required:true},
FIELD3: {type:String, required:true}
});
你可以看到,我真的想在DB的钥匙,更描述它们包含的值比它们在源json中的值更高(即,我希望json中的FIELD1
在DB中为date
)。
这样做的最好方法是什么?我见过猫鼬aliasfield,这是最好的方法还是有默认的方式来定义模式中的FIELD1 as
someothername?
虽然我有你,那么将FIELD1和FIELD3分别作为Date
和Number
分别从json源代码中的当前字符串中投射出来的正确方法是什么? ;)
感谢。但是,上面只是定义了字段所需的类型吗?如果我从具有该值的源以字符串的形式导入,那么也会将其转换为“Date”等等? –
是的,可以看到我更新的答案。 –
谢谢。关于在模式定义中具有不同键的选项,比在输入json中是唯一的/最好的选项https://github.com/ramiel/mongoose-aliasfield –