0
我有對象即數組的數據,如何在貓鼬中動態創建文檔集合?
$arrayObj=[{name:'Jack'},{name:'Ram'},{name:'Sham'}];
現在,我需要建立動態的收集與「MyCollection的」集合名稱。之後,收集想是: -
MyCollection的=> {名:傑克},{名稱:拉姆},{名稱:深水}
我知道如何使用Model
插入數據。
我有對象即數組的數據,如何在貓鼬中動態創建文檔集合?
$arrayObj=[{name:'Jack'},{name:'Ram'},{name:'Sham'}];
現在,我需要建立動態的收集與「MyCollection的」集合名稱。之後,收集想是: -
MyCollection的=> {名:傑克},{名稱:拉姆},{名稱:深水}
我知道如何使用Model
插入數據。
得到了一個解決方案:
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();