Mongoose查询无法构造

时间:2018-05-23 14:07:48

标签: javascript mongodb express mongoose mongoose-schema

我在使用mongoose创建复杂查询时遇到了一些困难。

基本上我需要找到有四个条件的所有职业选手:

  • 通过区域
  • 通过服务
  • 通过开始DateTime
  • 和一个小时(持续时间)

被击中的人已经排好了。

Pro模型如下所示:

let ProSchema = new Schema({
...
services: [
    {
        type: Schema.Types.ObjectId,
        ref: 'Service'
    }
],
zone: [
    {
        type: Schema.Types.ObjectId,
        ref: 'Zone'
    }
],
availability: [
    {
        day: {
            type: Number
        },
        from: {
            h: {
                type: Number
            },
            m: {
                type: Number
            },
            min_count: {
                type: Number
            }
        },
        to: {
            h: {
                type: Number
            },
            m: {
                type: Number
            }
            min_count: {
                type: Number
            }
        }
    }
],
unavailability: [
    {
        from: {
            type: Date
        },
        to: {
            type: Date
        }
    }
],
...

目前这就是我所取得的成就:

const date_start = new Date(req.body.date_start);
const date_finish = new Date(req.body.date_start + (1000 * 60 * 60 * req.body.duration));
Pro.find({
    services: req.body.service_id,
    zone: req.body.zone_id,
    "availability.day": parseInt(date_start.getDay(), 10)
})

这是我发送给服务器以计算在时间范围内可用的Pro(s)的对象:

{
    "date_start": 1527423607000,
    "duration": 3,
    "service_id": "5af0806f178d640aa5e58e4e",
    "zone_id": "5afeee28f64d273676d5d3b4"
}

我怎么说,通过moongose搜索在该时间范围内可用的专业人士(从date_start到date_start +持续时间)并且同时不是不可用的?

感谢有人甚至考虑帮助我

编辑:该集合就像这样(专业版)

    /* 1 */
{
    "_id" : ObjectId("5ae1fef5ff64714a5674d332"),
    "services" : [ 
        ObjectId("5af0807e178d640aa5e58e6c"), 
        ObjectId("5af0806e178d640aa5e58e4d")
    ],
    "zone" : [],
    "reviews_value" : {
        "value" : 1,
        "sum" : 2,
        "num" : 2
    },
    "availability" : [],
    "unavailability" : [],
    "status" : "pending",
    "hashtag" : "#peppe",
    "bio" : "sono peppe nuovo",
    "strengths" : [ 
        ObjectId("5ae1ffb8d93142a5515672b5"), 
        ObjectId("5ae20004d93142a5515672b6"), 
        ObjectId("5ae2000dd93142a5515672b7")
    ],
    "level" : 0,
    "user" : ObjectId("5aef323ed93142a5515672bb"),
    "updated_at" : ISODate("2018-04-29T09:40:54.405Z"),
    "created_at" : ISODate("2018-04-26T16:31:49.900Z"),
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("5ae1ff17ff64714a5674d333"),
    "services" : [],
    "zone" : [],
    "reviews_value" : {
        "value" : 5,
        "sum" : 5,
        "num" : 1
    },
    "availability" : [],
    "unavailability" : [],
    "status" : "confirmed",
    "hashtag" : "#simonescemo",
    "bio" : "sono gian ",
    "strengths" : [],
    "level" : 0,
    "user" : ObjectId("5ad85b73c8c65423ee126313"),
    "updated_at" : ISODate("2018-05-06T17:38:52.297Z"),
    "created_at" : ISODate("2018-04-26T16:32:23.710Z"),
    "__v" : 0
}

/* 3 */
{
    "_id" : ObjectId("5ae5946a8c09fc67c714597d"),
    "services" : [ 
        ObjectId("5af0806f178d640aa5e58e4e"), 
        ObjectId("5af08073178d640aa5e58e56")
    ],
    "zone" : [ 
        ObjectId("5afeee28f64d273676d5d3b4"), 
        ObjectId("5afeedccf64d273676d5d3b2")
    ],
    "reviews_value" : {
        "value" : 0,
        "sum" : 0,
        "num" : 0
    },
    "availability" : [ 
        {
            "from" : {
                "h" : 12,
                "m" : 30
            },
            "to" : {
                "h" : 20,
                "m" : 0
            },
            "_id" : ObjectId("5afee5f7f64d273676d5d3aa"),
            "day" : 0
        }, 
        {
            "from" : {
                "h" : 12,
                "m" : 0
            },
            "to" : {
                "h" : 15,
                "m" : 0
            },
            "_id" : ObjectId("5b03e3f563ca2316fd4765ec"),
            "day" : 6
        }
    ],
    "unavailability" : [ 
        {
            "_id" : ObjectId("5b03e3f563ca2316fd4765eb"),
            "from" : ISODate("2018-05-30T22:00:29.000Z"),
            "to" : ISODate("2018-05-31T21:59:29.000Z")
        }
    ],
    "status" : "disabled",
    "hashtag" : "",
    "bio" : "",
    "strengths" : [],
    "level" : 0,
    "user" : ObjectId("5aabdc753ca3ca3065309983"),
    "updated_at" : ISODate("2018-04-29T09:46:18.378Z"),
    "created_at" : ISODate("2018-04-29T09:46:18.378Z"),
    "__v" : 0
}

/* 4 */
{
    "_id" : ObjectId("5ae5949b8c09fc67c714597e"),
    "services" : [],
    "zone" : [],
    "reviews_value" : {
        "value" : 0,
        "sum" : 0,
        "num" : 0
    },
    "availability" : [],
    "unavailability" : [],
    "status" : "pending",
    "hashtag" : "",
    "bio" : "",
    "strengths" : [],
    "level" : 0,
    "user" : ObjectId("5ae1f9fb77a98944b7bf94ad"),
    "updated_at" : ISODate("2018-04-29T09:47:07.334Z"),
    "created_at" : ISODate("2018-04-29T09:47:07.334Z"),
    "__v" : 0
}

/* 5 */
{
    "_id" : ObjectId("5afc563496b3c8744e03a2cb"),
    "services" : [],
    "zone" : [],
    "reviews_value" : {
        "value" : 0,
        "sum" : 0,
        "num" : 0
    },
    "availability" : [],
    "unavailability" : [],
    "status" : "pending",
    "hashtag" : "",
    "bio" : "",
    "strengths" : [],
    "level" : 0,
    "user" : ObjectId("5ae1fa3c77a98944b7bf94ae"),
    "updated_at" : ISODate("2018-05-16T16:03:00.918Z"),
    "created_at" : ISODate("2018-05-16T16:03:00.918Z"),
    "__v" : 0
}

/* 6 */
{
    "_id" : ObjectId("5afc568996b3c8744e03a2cc"),
    "services" : [],
    "zone" : [],
    "reviews_value" : {
        "value" : 0,
        "sum" : 0,
        "num" : 0
    },
    "availability" : [],
    "unavailability" : [],
    "status" : "pending",
    "hashtag" : "",
    "bio" : "",
    "strengths" : [],
    "level" : 0,
    "user" : ObjectId("5ae1fa7177a98944b7bf94af"),
    "updated_at" : ISODate("2018-05-16T16:04:25.742Z"),
    "created_at" : ISODate("2018-05-16T16:04:25.742Z"),
    "__v" : 0
}

/* 7 */
{
    "_id" : ObjectId("5afc56aa96b3c8744e03a2cd"),
    "services" : [],
    "zone" : [],
    "reviews_value" : {
        "value" : 0,
        "sum" : 0,
        "num" : 0
    },
    "availability" : [],
    "unavailability" : [],
    "status" : "pending",
    "hashtag" : "",
    "bio" : "",
    "strengths" : [],
    "level" : 0,
    "user" : ObjectId("5aec761583c4ee2fece38f93"),
    "updated_at" : ISODate("2018-05-16T16:04:58.344Z"),
    "created_at" : ISODate("2018-05-16T16:04:58.344Z"),
    "__v" : 0
}

/* 8 */
{
    "_id" : ObjectId("5afc56c296b3c8744e03a2ce"),
    "services" : [],
    "zone" : [],
    "reviews_value" : {
        "value" : 0,
        "sum" : 0,
        "num" : 0
    },
    "availability" : [],
    "unavailability" : [],
    "status" : "pending",
    "hashtag" : "",
    "bio" : "",
    "strengths" : [],
    "level" : 0,
    "user" : ObjectId("5ae1fa9177a98944b7bf94b0"),
    "updated_at" : ISODate("2018-05-16T16:05:22.920Z"),
    "created_at" : ISODate("2018-05-16T16:05:22.920Z"),
    "__v" : 0
}

/* 9 */
{
    "_id" : ObjectId("5afc56e796b3c8744e03a2cf"),
    "services" : [],
    "zone" : [],
    "reviews_value" : {
        "value" : 0,
        "sum" : 0,
        "num" : 0
    },
    "availability" : [],
    "unavailability" : [],
    "status" : "pending",
    "hashtag" : "",
    "bio" : "",
    "strengths" : [],
    "level" : 0,
    "user" : ObjectId("5add8f4b6ad16e39f79b44f6"),
    "updated_at" : ISODate("2018-05-16T16:05:59.086Z"),
    "created_at" : ISODate("2018-05-16T16:05:59.086Z"),
    "__v" : 0
}

编辑:经过一天的思考后,我添加了一个min_count给模型,现在查询现在可用于提供:

const date_start = new Date(req.body.date_start);
    const date_finish = new Date(req.body.date_start + (1000 * 60 * 60 * req.body.duration));
    const min_count_from = (date_start.getHours() * 60) + date_start.getMinutes();
    const min_count_to = (date_finish.getHours() * 60) + date_finish.getMinutes();

Pro.find({
        services: req.body.service_id,
        zone: req.body.zone_id,
        "availability.day": parseInt(date_start.getDay(), 10),
        $and: [{
                "availability.from.min_count": {
                    $lte: min_count_from
                }
            },
            {
                "availability.to.min_count": {
                    $gte: min_count_to
                }
            }
        ],
        $and: [{
            "unavailability.to": {
                $gt: date_start
            }
        }, {
            "unavailability.from": {
                $lt: date_finish
            }
        }]

    })

问题在于这件作品无效:

     $and: [{
                "unavailability.to": {
                    $gt: date_start
                }
            }, {
                "unavailability.from": {
                    $lt: date_finish
                }
            }]

我做错了什么?

0 个答案:

没有答案