Reputation: 3427
Two questions here.
* Example *
var ProductSchema = new Schema({
_id: Schema.Types.ObjectId,
product_name: String
});
var purchaseOrderSchema = new Schema(
{
purchaseOrderNo: Number,
products: [ProductSchema]
}
);
const purchaseOrder = new PurchaseOrder(req.body);
PurchaseOrder.findOneAndUpdate(
{ _id: req.body._id },
{
$set: req.body,
$push: req.body.products
},
{ upsert: true, new: true }
)
.then((result) => {
console.log('result', result);
res.status(200).json(result);
})
.catch((err) => {
console.log('err', err);
res.status(500).json({ error: err });
});
const body = {
_id: 'skjdhflksjdf',
purchaseOrderNo: 1,
products: [
{
_id: '111',
product_name: 'Cup'
},
{
_id: '222',
product_name: 'Spoon'
}
]
}
Upvotes: 0
Views: 143
Reputation: 2011
In the ProductSchema
the type
of _id
field to set to ObjectId
. The product id 111 and 222 are not a valid ObjectId
and it fails to cast it. You can update the type of _id
in ProductSchema
to Number
for this to work
var ProductSchema = new Schema({
_id: Number,
product_name: String
});
Upvotes: 1