These are chat archives for Automattic/mongoose

16th
Jan 2016
Diego Aguilar Aguilar
@diegoaguilar
Jan 16 2016 17:49

I'm trying following code for finding overlapping events according to documents in a collection:

Calendar.find({
        facility: calendar.provider,
        provider: calendar.facility,
        endDate: {$gt: moment(startDate)},
        date: {$lt: moment(endDate)}
    },
    function(err, calendars) {
        console.log(calendars);
        if (options && 'skip' in options) {
            calendars = calendars.filter((c) => c._id !== options.skip._id);
        }
        console.log(calendars);
        calendars.filter((c) => c.open == false);
        console.log(calendars);
        return cb(err, calendars.length > 0);
    }
);

This is returning calendars as n empty array, I got Mongoose debug mode activated so I'm seeing the actual performed query:

Mongoose: calendars.find({ date: { '$lt': new Date("Fri, 29 Jan 2016 16:32:00 GMT") }, endDate: { '$gt': new Date("Fri, 29 Jan 2016 16:17:00 GMT") }, provider: ObjectId("569a6dca7c147af44e1b515c"), facility: ObjectId("569a6db67c147af44e1b50ef") }) { fields: undefined }

The strage thing is that I can do at Mongo CLI (for SAME DB and I'm complety sure all docs got same provider and facility, so):

> var start = new Date("Fri, 29 Jan 2016 16:17:00 GMT")
> var end = new Date("Fri, 29 Jan 2016 16:32:00 GMT")
> db.calendars.find({endDate: {$gt: start}, date: {$lt: end}})
{ "_id" : ObjectId("569a6f597c147af44e1b5165"), "provider" : ObjectId("569a6db67c147af44e1b50ef"), "date" : ISODate("2016-01-29T16:20:00Z"), "duration" : 15, "facility" : ObjectId("569a6dca7c147af44e1b515c"), "open" : false, "user" : ObjectId("569a6db67c147af44e1b50ef"), "endDate" : ISODate("2016-01-29T16:35:00Z"), "updatedAt" : ISODate("2016-01-16T16:27:05Z"), "createdAt" : ISODate("2016-01-16T16:27:05Z"), "__v" : 0 }
{ "_id" : ObjectId("569a708d7c147af44e1b5167"), "provider" : ObjectId("569a6db67c147af44e1b50ef"), "date" : ISODate("2016-01-29T16:30:00Z"), "duration" : 15, "facility" : ObjectId("569a6dca7c147af44e1b515c"), "open" : false, "user" : ObjectId("569a6db67c147af44e1b50ef"), "endDate" : ISODate("2016-01-29T16:45:00Z"), "updatedAt" : ISODate("2016-01-16T16:32:13Z"), "createdAt" : ISODate("2016-01-16T16:32:13Z"), "__v" : 0 }
{ "_id" : ObjectId("569a7b1cee9657c85fa6d260"), "provider" : ObjectId("569a6db67c147af44e1b50ef"), "date" : ISODate("2016-01-29T16:17:00Z"), "duration" : 15, "facility" : ObjectId("569a6dca7c147af44e1b515c"), "open" : false, "user" : ObjectId("569a6db67c147af44e1b50ef"), "endDate" : ISODate("2016-01-29T16:32:00Z"), "updatedAt" : ISODate("2016-01-16T17:17:16Z"), "createdAt" : ISODate("2016-01-16T17:17:16Z"), "__v" : 0 }

Same DB, same collection, same query params, and it works properly with results. How is this even possible?

Diego Aguilar Aguilar
@diegoaguilar
Jan 16 2016 18:26
NVM my bad, I got:
Calendar.find({
        facility: calendar.provider,
        provider: calendar.facility,
        endDate: {$gt: moment(startDate)},
        date: {$lt: moment(endDate)}
    },
facility is trying to match provider and provider matching facility
:(
my bad
Joseph Thibeault
@jrthib
Jan 16 2016 20:21
hey, i’m reading the docs on hooks. Can post hooks be ran async?
just like pre-save