在数组中查询日期 [英] Query in Array with dates

查看:66
本文介绍了在数组中查询日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在array("availability")中进行查询(在MongoDB中),该查询将返回唯一一个元素("available")等于1且位于可用性内部日期之间的酒店.

I'm trying to do a query(In MongoDB) in array("availability") that will return a only hotel that have the element("available") equals 1 and between the dates inside the availability.

但是当正确的返回值为"Mercato Hotel"时,查询将返回所有酒店

But the query return all hotels when the correct return is "Mercato Hotel"

我使用的查询没有成功:

Query that i have used without success:

{city: "Boston", availability: { $elemMatch: {availability: 1, date: {$gte: ISODate("2015-05-02T00:00:00.000+0000")}, date: {$lte: ISODate("2015-05-04T00:00:00.000+0000")}}}}

MongoDb中的Json:

Json in MongoDb:

 { 
        "_id" : ObjectId("55b302ee8debdf1a908cdc85"), 
        "city" : "Boston", 
        "hotel" : "Mercatto Hotel", 
        "availability" : [
            {
                "date" : ISODate("2015-05-01T00:00:00.000+0000"), 
                "available" : NumberInt(0)
            }, 
            {
                "date" : ISODate("2015-05-02T00:00:00.000+0000"), 
                "available" : NumberInt(0)
            }, 
            {
                "date" : ISODate("2015-05-03T00:00:00.000+0000"), 
                "available" : NumberInt(0)
            }, 
            {
                "date" : ISODate("2015-05-04T00:00:00.000+0000"), 
                "available" : NumberInt(1)
            }
        ]
    }
    { 
        "_id" : ObjectId("55b302ee8debdf1a908cdc89"), 
        "city" : "Boston", 
        "hotel" : "Hostel Villa", 
        "availability" : [
            {
                "date" : ISODate("2015-05-01T00:00:00.000+0000"), 
                "available" : NumberInt(1)
            }, 
            {
                "date" : ISODate("2015-05-02T00:00:00.000+0000"), 
                "available" : NumberInt(0)
            }, 
            {
                "date" : ISODate("2015-05-03T00:00:00.000+0000"), 
                "available" : NumberInt(0)
            }, 
            {
                "date: ISODate("2015-05-04T00:00:00.000+0000"), 
                "available" : NumberInt(0)
            }
        ]
    }

有人可以帮助我吗?

谢谢...

推荐答案

查询:

{
    city: "Boston", 
    availability: {
            $elemMatch: {
                    available: 1, 
                    date: {
                        $gte: ISODate("2015-05-02T00:00:00.000+0000"), 
                        $lte: ISODate("2015-05-04T00:00:00.000+0000")
                    }
            }
    }
}

返回相同的结果.换句话说,当正确的返回值为"Mercato Hotel"时,返回所有酒店.

Returned the same result. In other words, returned all hotels when the correct return is "Mercato Hotel".

这篇关于在数组中查询日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆