// Create a route for the create API
router.post('/create', async (req, res, next) => {
try {
const { _id, name } = req.body;
// Check if _id or name already exist
const existingDocument = await YourModel.findOne({ $or: [{ _id }, { name }] });
if (existingDocument) {
if (existingDocument._id === _id) {
return res.status(400).json({ error: 'Document with the same _id already exists.' });
} else {
return res.status(400).json({ error: 'Document with the same name already exists.' });
}
}
// Create a new document
const newDocument = new YourModel({ _id, name });
await newDocument.save();
return res.status(201).json({ message: 'Document created successfully' });
} catch (error) {
next(error); // Pass the error to the error handling middleware
}
});