I am using MongoDb with Mongoskin. In the collection I save events. Among other fields, these events have a start and an end, stored as Dates in Mongodb.
events { start: "Date1", end: "Date2", ... }
When inserting new documents into this collection, I need a delimiter that prohibits the insertion of a document, which start dates overlap the created case. In short, I do not want events to have the same time span.
Question: Is there a way to deal with this restriction through MongoDb with some unique index? I think not, but please correct me if I am wrong!
If not:
Question Do I have to check for possible code overlaps before inserting new events? Do I need to configure some kind of write lock so that another user cannot compress the event between the time that I check for overlaps and the input of my own event? How is this done in MongoDb?
EDIT
This is the best way that I have reached so far, in fact it works very well.
var input = getPostInput(); var query = {$and: [ {start: {$lte: input.end}}, {end: {$gte: input.start}} ]}; db.events.findAndModify(query, {}, {$setOnInsert: input}, {new: true, upsert: true}, callback)
It uses findAndModify as the type of the findOrCreate statement. $setOnInsert add POST input properties only if findAndModify does not find the document, and upsert: true says that it should create the document if it is not found. The two options in combination seem to create the findOrCreate statement.
EDIT
Problems arise when updating (PUT) events. I cannot reuse the code above because it relies on upsert and $setOnInsert .
EDIT
@wdberkeley:
I am still afraid of this basic problem: to ensure uniqueness in the range . The more I think about it, it seems that an “array of time slices” may be the most problematic solution. For example, suppose that 5 minutes is selected as the minimum time period and the average booking is 45 minutes. This would require me to save 9 numbers (possibly date s): timespan = [0,5,10,15,20,25,30,35,40] instead of two: start=0, end=45 . This is more than four times the stored data for an average reservation. I don’t want to be harsh, but don’t you see this as a problem? Or does it become a problem when the stored data is more than 10 times larger or 100 times larger? I really understand that this also applies to the total number of orders made ...