如何在mongoose中动态创建文档集合?

时间:2016-05-17 09:02:07

标签: node.js mongodb mongoose

我有对象数组的数据,即

$arrayObj=[{name:'Jack'},{name:'Ram'},{name:'Sham'}];

现在,我需要使用'mycollection'集合名称动态创建集合。之后的收集应该是: -

  

MyCollection的=> {name:Jack},{name:Ram},{name:Sham}

我知道如何使用Model插入数据。

1 个答案:

答案 0 :(得分:0)

有一个解决方案:

var thingSchema = new Schema({}, { strict: false, collection: 'mycollection' });

 //strict, if true then values passed to our model constructor that were not specified in our schema do not get saved to the db.
//collection, for prevent from auto append 's'.

var Thing = mongoose.model('mycollection', thingSchema);
var thing = new Thing($arrayObj);
thing.save();
相关问题