I've got this code that goes on over and over again. It inserts a new document into a Meteor Mongo collection called Services
, which is a global object that is instantiated already in another file (Services = new Mongo.Collection("services")
).
Services.insert({
sku: 'hdrPhotos',
price: 100
});
Services.insert({
sku: 'twilightPhotos',
price: 100
});
Services.insert({
sku: 'videoClips',
price: 175
});
I want to write a function that takes the collection name and an array of objects to insert, but I'm not sure how to reference the collection name as a variable in my function:
var insertIntoCollection = function(collectionName, arrayOfObjects){
for (index in arrayOfObjects){
// doesn't work
// collectionName.insert(arrayOfObjects[index]);
};
};
It would be called like
var serviceItems = [{
sku: 'hdrPhotos',
price: 100
},{
sku: 'twilightPhotos',
price: 100
},{
sku: 'videoClips',
price: 175
}];
insertIntoCollection("Services", serviceItems);