My Question is related to Mongo DB - embedded array vs separate document.
I have following requirement.
An event can have multiple instances of dates(Start Date- End Date), in which for each instance the event details are same except events start date and end date.
User can add many instances per event.
So in Mongo DB which of the following Structure would be suitable.
Having separate record/document for each instance.
Maintain array of instances in event document only.
Thanks in Advance.
Amol
Well I would've gone for second option i-e
Maintain array of instances in event document only.
Because of the following
This would allow me manage the data in more efficient way like if I want to update name of the event for example, I won't have to update in all the rows if I had used the first option. In second case I just need to update a single row.
If I had to delete an event, again I'll have to deal with only one document in second case and in first case, I had to delete all those rows with that event
Don't know how I would have managed to mark different documents as the same event in the first case, instead, In second case I'd always have one unique Id for a particular event.
If I need I can unwind the document to number of documents, to use aggregation for example.
Related
I am using the Firestore DB with a Flutter application.
In the application, I schedule employees to shifts for the following days.
As a feature, I want to implement a calendar that visualizes employee availability. When an employee is not assigned to a shift or not away he is available.
At the moment I have a collection of Shifts and Employees within Firebase. I was thinking of creating an Unavailability collection but on a second thought that might be unnecessary.
The question is, should I create an Unavailability collection within Firebase? Each document would have a property of date, employee name, and reason (employee already assigned, sick, vacation, etc.) Or implement this within the Employees collection which already exists. Within this collection, I have stored employee's attributes such as name, salary, email...
My concern with this is that for a given employee document, I will have an attribute with a very long list that contains the unavailability dates. Also, I would like to present the reason for the unavailability. This means I will have to present a shift with its details if the employee is already assigned to a shift or the general unavailability details.
Should I instead implement this within Flutter (3 collections for 1st case)?
Note: If it matters, I am using Bloc for the state management within Flutter.
I have a CRUD that contains 5 tables and I need to do some processing on the server side alter the last table has been updated, the events on the datasetprovider happens after each individual table.
Like, I have 3 tables, person, car, personcar. I fill the fields I click save, first it's gonna save the person, then the car and last the personcar.
How can I know, in the transaction block, the last table?
I couldn't find the right 'after' event...
there's no such built-in functionality unless you utilize nested datasets that is. you should either implement some specific remote method wrapping all required modifications or enhance midas with extra means (see for example "KT Data Components" lib to get the idea how this could be implemented)
Your transaction should be on the serverside...have a method call that passes all your datasets in one call...handles the start/commit/rollback on the server side not the client side.
I am using mgo driver for MongoDB under Go.
My application asks for a task (with just a record select in Mongo from a collection called "jobs") and then registers itself as an assignee to complete that task (an update to that same "job" record, setting itself as assignee).
The program will be running on several machines, all talking to the same Mongo. When my program lists the available tasks and then picks one, other instances might have already obtained that assignment, and the current assignment would have failed.
How can I get sure that the record I read and then update does or does not have a certain value (in this case, an assignee) at the time of being updated?
I am trying to get one assignment, no matter which one, so I think I should first select a pending task and try to assign it, keeping it just in the case the updating was successful.
So, my query should be something like:
"From all records on collection 'jobs', update just one that has assignee=null, setting my ID as the assignee. Then, give me that record so I could run the job."
How could I express that with mgo driver for Go?
This is an old question, but just in case someone is still watching at home, this is nicely supported via the Query.Apply method. It does run the findAndModify command as indicated in another answer, but it's conveniently hidden behind Go goodness.
The example in the documentation matches pretty much exactly the question here:
change := mgo.Change{
Update: bson.M{"$inc": bson.M{"n": 1}},
ReturnNew: true,
}
info, err = col.Find(M{"_id": id}).Apply(change, &doc)
fmt.Println(doc.N)
I hope you saw the comments on the answer you selected, but that approach is incorrect. Doing a select and then update will result in a round trip and two machines and be fetching for the same job before one of them can update the assignee. You need to use the findAndModify method instead: http://www.mongodb.org/display/DOCS/findAndModify+Command
The MongoDB guys describe a similar scenario in the official documentation: http://www.mongodb.org/display/DOCS/Atomic+Operations
Basically, all you have to do, is to fetch any job with assignee=null. Let's suppose you get the job with the _id=42 back. You can then go ahead and modify the document locally, by setting assignee="worker1.example.com" and call Collection.Update() with the selector {_id=42, assignee=null} and your updated document. If the database is still able to find a document that matches this selector, it will replace the document atomically. Otherwise you will get a ErrNotFound, indicating that another thread has already claimed the task. If that's the case, try again.
I'm using meteor. I'm wondering if theres a shorthand way to do batch updates before the DOM is updated.
for instance I want to update some records,more than one (All at once):
Collection.update(id1,{..})
Collection.update(id2,{..})
Collection.update(id3,{..})
The problem is there are 3 items being updated separately. So when the DOM in my case was being redrawn 3 times instead of once (with all 3 updated records).
Is there a way to hold off the ui updating until all of them are updated?
Mongo's update can modify more than one document at a time. Just give it a selector that matches more than one document, and set the multi option. In your case, that's just a list of IDs, but you can use any selector.
Collection.update({_id: {$in: [id1, id2, id3]}}, {...}, {multi:true});
This will run a single DB update and a single redraw.
Execute them on the server instead, that way they might be synchronously done such that they are less likely to cause multiple DOM updates on the client.
See the first two and last interesting code bits, which explain how to protect your clients from messing with the database as well as how to define methods on the server and call them from the client.
What I'm trying to accomplish is the following: I need to limit the amount of core data entries to 50. So if the user enters their 50th entry then the app would delete the oldest entry and add the new entry to the top of the stack. So basically, if the user never deletes entries and if there are 50 entries in core data then, when the user tries to add a new entry, the app would delete the oldest entry and add the user's new entry. Basically, I'm trying to have a history sort of thing but I don't want the user to be able to go past 50 entries however I want them to be able to add new entries when their at the 50 limit by just dropping the oldest one and adding the newest one. What would be the easiest way to do this? I'm new to core data and having a hard time understanding a lot of it. Here's the code / example app that I'm working with. LINK TO EXAMPLE APP THAT I'M USING Thanks for the help.
Let's say you have an entity called History. The easiest solution would be to add a creationDate attribute to your entities. Then use that to manage your History objects.
You will need three fetches:
The first one will fetch as faults all the existing History objects and then count them. If the count is <50, then just add the new History object and your done.
If the count>=50, then do a fetch for specific value and use the #max or #min (I forget which for dates) collections operator to find the oldest creationDate. (As luck would have it the example at the link it pretty much exactly what you need.)
Perform a fetch for the object with the creationDate returned by (2) and delete it.
Then add the new history object.
OK, that's fine. CoreData is not going to do this for you, but you can do it yourself.
You can retrieve objects from you context using an NSFetchRequest, and you can delete them using -[NSManagedObjectContext deleteObject:]. You can sort them using NSSortDescriptor objects.