i have model
const cartSchema = new Schema<ICart>(
{
orders: {
type: [
{
hotelId: {
type: SchemaTypes.ObjectId,
required: true,
ref: 'hotels',
},
startDate: { type: Date, required: true },
endDate: { type: Date, required: true },
rooms: {
type: [
{
roomTypeId: {
type: SchemaTypes.ObjectId,
required: true,
ref: 'roomTypes',
},
quantity: {
type: Number,
min: 1,
required: true,
},
},
],
required: true,
min: 1,
},
},
],
required: true,
max: 20,
min: 1,
},
userId: {
type: SchemaTypes.ObjectId,
index: true,
unique: true,
required: true,
ref: 'users',
},
isActive: { type: Boolean, default: true, required: true },
},
{ timestamps: true, collection: 'carts' },
);
const Cart = model<ICart>('carts', cartSchema);
Cart .findOneAndUpdate(
{
userId,
orders: {
$elemMatch: {
hotelId: newOrder.hotelId,
startDate: newOrder.startDate,
endDate: newOrder.endDate,
},
},
},
{
$set: { 'orders.$': newOrder },
},
{ new: true },
);
i want if orders have hoteiId startDate, endDate same value in newOrder it will update, if not , it insert new newOrder , but not work