Query in Array with dates

独自空忆成欢 提交于 2020-03-16 05:46:38

问题


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.

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")}}}}

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)
            }
        ]
    }

Someone can help me?

Thanks...


回答1:


You have got a typo in your query, availability instead of available, should be this:

{
    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")
                    }
            }
    }
}

UPDATE with Blakes Seven

If you want to get only the element of availability array that matches your query, add projection:

{
    "city": 1,
    "hotel": 1
    "availability.$.date": 1
}



回答2:


The query:

{
    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")
                    }
            }
    }
}

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




回答3:


You can use aggregation to get expected output as following:

db.collection.aggregate({
    $unwind: "$availability"
}, {
    $match: {
    "city": "Boston",
    "availability.available": 1,
    "availability.date": {
        $gte: ISODate("2015-05-02T00:00:00.000+0000"),
        $lte: ISODate("2015-05-04T00:00:00.000+0000")
    }
    }
})

Edit

If there are multiple available=1 then use following query:

db.collection.aggregate({
    $unwind: "$availability"
}, {
    $match: {
    "city": "Boston",
    "availability.available": 1,
    "availability.date": {
        $gte: ISODate("2015-05-02T00:00:00.000+0000"),
        $lte: ISODate("2015-05-04T00:00:00.000+0000")
    }
    }
}, {
    $group: {
    _id: "$hotel",
    "city": {
        $first: "$city"
    },
    "availability": {
        $push: "$availability"
    }
    }
})


来源:https://stackoverflow.com/questions/31622709/query-in-array-with-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!