vineet
vineet

Reputation: 14236

How to create collection with documents dynamically in mongoose?

I have data of array of objects i.e,

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

Now, i need to create collection dynamically with 'mycollection' collection name. After that collection should like be:-

mycollection=> {name:Jack}, {name:Ram}, {name:Sham}

I know how to insert data by using Model.

Upvotes: 0

Views: 1609

Answers (1)

vineet
vineet

Reputation: 14236

Got a solution:

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();

Upvotes: 1

Related Questions