I want to update a record by its id but before update, i want to delete the previously stored data on that same record.What would be the process?
controller.ts
public updateVendorServiceSpecialPrice = async (req: Request, res: Response, next: NextFunction): Promise<Response | void> => {
try {
if (req.body.vendorServiceId!='') {
let results = await this.ServicesService.updateVendorServicesSpecialPrice(req.params.vendorServiceId, req.body);
if (results != false) {
this.success(res, 'Updated Successfully', 200, results._id);
}
}
return await this.error(res, 'Something Went Wrong!.', 500);
} catch (e) {
next(e)
}
}
service.ts
public async updateVendorServicesSpecialPrice(
vendorServiceId: any,
data: any
): Promise<any | Error> {
try {
return new Promise((resolve, reject) => {
vendorServiceSpecialPriceSchema.findByIdAndUpdate(
vendorServiceId,
{ ...data },
(err: any, success: any) => {
if (err) {
reject(err);
}
if (!success) {
resolve(false);
} else {
resolve(success);
}
}
);
});
} catch (e) {
console.log('service error\n', e);
throw e;
}
}
I am trying in this way to solve this,may be I am wrong,what would br the right process:
public async updateVendorServicesSpecialPrice(
vendorServiceId: any,
data: any
): Promise<any | Error> {
try {
return new Promise((resolve, reject) => {
vendorServiceSpecialPriceSchema.deleteOne({vendorServiceId})
vendorServiceSpecialPriceSchema.findOneAndUpdate(
vendorServiceId,
{ ...data },
{ returnNewDocument: true },
(err: any, success: any) => {
if (err) {
reject(err);
}
if (!success) {
resolve(false);
} else {
resolve(success);
}
}
);
});
} catch (e) {
console.log('service error\n', e);
throw e;
}
}
schema:
const SpecialPriceSchema = new Schema({
_id: { type: Schema.Types.ObjectId },
vendorServiceId: { type: String, ref: 'vendorService', index: true },
vendorId: { type: String },
startDate: { type: String },
endDate: { type: String },
recurrenceType: { type: Number }, //1-All Day,2 weekend,3 custom
priceType: { type: Number }, //1- total,2 per piece/per person,3 per hour
minGuestRange: { type: Number, default: 0 },
maxGuestRange: { type: Number, default: 0 },
price: { type: Number, default: 0 },
minCapacity: { type: Number, default: 0 },
maxCapacity: { type: Number, default: 0 },
isBlocked: { type: Boolean, default: false },
isDeleted: { type: Boolean, default: false },
createdAt: { type: Date, default: Date.now() },
updatedAt: { type: Date, default: Date.now() }
})
let vendorServiceSpecialPriceSchema = model<IVendorSpecialPrice>('vendorServiceSpecialPrice', SpecialPriceSchema);
export { vendorServiceSpecialPriceSchema }
Schema and collection
Thanks for your time....
CodePudding user response:
What you are looking at is probably ReplaceOne, which replaces the entire document base on _id
const query = { _id: id };
const replacement = {
title: `The Cat from Sector ${Math.floor(Math.random() * 1000) }`,
};
const result = await movies.replaceOne(query, replacement);
You can replace a single document using the collection.replaceOne() method. replaceOne() accepts a query document and a replacement document. If the query matches a document in the collection, it replaces the first document that matches the query with the provided replacement document. This operation removes all fields and values in the original document and replaces them with the fields and values in the replacement document. The value of the _id field remains the same unless you explicitly specify a new value for _id in the replacement document. https://www.mongodb.com/docs/drivers/node/current/usage-examples/replaceOne/
CodePudding user response:
As suggested by @SomeoneSpecial, replaceOne
should be the correct approach:
public async updateVendorServicesSpecialPrice(
vendorServiceId: any,
data: any
): Promise<any | Error> {
try {
return new Promise((resolve, reject) => {
vendorServiceSpecialPriceSchema.replaceOne(
{ vendorServiceId },
data,
(err: any, docs: any) => {
if (err) {
reject(err);
}
if (!docs) {
resolve(false);
} else {
resolve(docs);
}
}
);
});
} catch (e) {
console.log('service error\n', e);
throw e;
}
}