0

I have documents likes the following:

{
  "_id": "538584aad48c6cdc3f07a2b3",
  "startTime": "2014-06-12T21:30:00.000Z",
  "endTime": "2014-06-12T22:00:00.000Z",
},
{
  "_id": "538584b1d48c6cdc3f07a2b4",
  "startTime": "2014-06-12T22:30:00.000Z",
  "endTime": "2014-06-12T23:00:00.000Z",
}

As you can see the documents above have startTime and endTime. I need to update some document that don't overlap others. I can make things to work by using two queries:

var event_id = "538584b1d48c6cdc3f07a2b4";
var event = {
    startTime: "2014-06-12T21:30:00.000Z"
    endTime: "2014-06-12T23:30:00.000Z"
};

Model.count({
  _id: {$ne: event_id },
   "$or": [
            {"$and": [
                {"startTime":{"$lte":event.startTime},"endTime":{"$gt":event.startTime}},
                {"startTime":{"$lte":event.endTime},"endTime":{"$lt":event.endTime}}
            ]},
            {"$and":[
                {"startTime":{"$lte":event.startTime},"endTime":{"$gte":event.startTime}},
                {"startTime":{"$lte":event.endTime},"endTime":{"$gte":event.endTime}}
            ]}
        ]

}, function (err, count) {
     if (err) return next(err);

        if (count) {
            return next(new Error('Event overlapping'));
        }

        return Model.findOneAndUpdate({_id: event_id}, event, function (err, event) {
            if (err) return next(err);

            return res.json(200, event);
        });
});

As you can see from the code above I do first query for checking of existing event that could overlaps. And then I do update.

Is it possible to make updating by using single query?

Erik
  • 14,060
  • 49
  • 132
  • 218
  • This is not duplicate because in this post I've asked about updating instead inserting. This is different case. – Erik Jun 03 '14 at 08:29
  • This is so a duplicate en if only based on you have not understood your previous answers to questions you have asked on the same principles. Understand the answers given there first before asking additional questions. – Neil Lunn Jun 03 '14 at 11:23
  • I do understand the answer. This question is some different. – Erik Jun 03 '14 at 11:40
  • this question is not a duplicate since it's possible to insert based on match using upsert, but it's not nearly as easy for update based on _other_ documents. But it's true the "how to do the query" is duplicate. – Asya Kamsky Jun 03 '14 at 19:11
  • I can rename this post to something different – Erik Jun 03 '14 at 19:13
  • please don't rename - let the process do the right thing - if you keep adding and renaming and referring to different questions from other questions, it just increases the chances your questions will be closed. – Asya Kamsky Jun 03 '14 at 19:14
  • Ok thx. Do you have answer to this question? – Erik Jun 03 '14 at 19:20
  • can you clarify what the expected schema is? Do you expect to find this event already and if that's the case would it already have different start and end times? Or does it exist without any times set? – Asya Kamsky Jun 07 '14 at 18:17

0 Answers0