I have connected with MongoDB using below code.
mongoose.connect('mongodb://localhost:27017/samplecustomfields')
.then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err));
I have created the schema model and load model in my app.js file. You can see below code
const customfields = [
{
'id': 104,
'name': '[Sample] Utility Caddy',
'customfields': [
{
'id': 7,
'product_id': 104,
'name': 'Sample Utility',
'text': 'canddy ofsuc'
}
}
]
require('./models/customfieldlists');
const productfields = mongoose.model('fields');
app.get('/api/products', (req, res, next) => {
productfields.insertMany(customfields, function(error, docs) {
console.log('done');
});
res.send(customfields);
});
How can I drop my "fields" collection first and after call "insertMany" query. This should happend everytime when I get the '/api/products' this request.
I have put the code "productfields.dropCollection('fields');" above insertMany query but it does not work.