-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
I have a List<Employee> where each Employee has a Manager assigned. So for each object has a Manager reference in it, which is not a manager id, but a set of fields by which we can identify a manager.
Each Manager object has list of references of Employees
So classes look like this
Class Manager { ObjectId _id; //some attributes List<Employees> employees; LocalDateTime createdOn; }
Class Employee { ObjectId _id //some attributes Manager manager; //Having only attributes but not ObjectId of manager LocalDateTime createdOn; }
We have done this because names and some attributes of this manager can be changed and hence a new Manager Object can be introduced in the Manager collection and then all the existing employees will now belong to the new manager object. That’s how our structure is.
While querying, we will get attributes of a Manager, and we return back List of Employees of the manager who is latest according to the attributes and a createdOn field
So the problem which I’m facing is:
While saving a new List of Employees which belong to a new Manager, I first save all the Employees using insertMany() function of mongoClient. Then I save the Manager using insert().
If after saving the Employee list and before saving the Manager, something bad happened to Mongo and Db is down so I won’t be able to save the Manager, hence all Employees will now be orphaned.
What strategies I can follow in order to ensure that when MongoDB comes up again, I can first store all the pending objects and then continue with the rest of the operations.