My problem is that, I have a MongoDB collection named 'sold', which contains some string fields and some numeric fields.
const soldSchema = new Schema(
{
sku: String,
hsn: String,
qty: Number,
article: String,
mrp: Number,
disc: Number,
taxrate: Number,
tax: Number,
total: Number,
orderid: String,
},
{ collection: "sold", timestamps: true }
);
const Sold = mongoose.model("sold", soldSchema);
module.exports = { Customer, Stock, Sold, Order };
The 'Customer', 'Stock', and 'Order' are other schema are in the same module.
For the sold schema, I received the data from the front end, which is an array of objects like this as shown below:
[
{
sku: '10005',
hsn: '3652',
qty: '3',
article: 'tops',
mrp: '550',
disc: '0',
taxrate: '5',
tax: '82.50',
total: '1732.50',
orderid: '1633515982545'
},
{
sku: '10005',
hsn: '3652',
qty: '3',
article: 'tops',
mrp: '550',
disc: '0',
taxrate: '5',
tax: '82.50',
total: '1732.50',
orderid: '1633515982545'
},
{
sku: '10005',
hsn: '3652',
qty: '3',
article: 'tops',
mrp: '550',
disc: '0',
taxrate: '5',
tax: '82.50',
total: '1732.50',
orderid: '1633515982545'
}
]
The above array is the result of req.body i.e. console.log(req.body)
If I change all the fields to type 'String' in soldSchema, then all the values get inserted in a single go.
This function works fine with all string values in the schema.
async function insertManySold(req, res) {
let items = req.body;
try {
let result = await model.Sold.insertMany(items);
res.json(result);
} catch (error) {
res.json(error);
}
}
But, if I change the data types to Numbers for some fields in the schema as mentioned above, then it doesn't work. Please let me know how to achieve this.
I have searched many articles on the web but could not find the answer. Could you please help me?