I am aware of a similar question available in SO but mine is little different Find values in mongdb. I am trying out a hotel booking app. Here I store the info about on which date the rooms are booked, which means the rooms are available in all other dates. I am working with mongodb and python.
I am using the $nin function. I have a option called alternate dates, where the user gives in another date. I want to display the availability of only one day that is, if room is available on the first date then the search should stop if not it should continue to the next date and give result if available.
This solution gives result for all the given dates. solution of Find values in mongdb
The data stored is
{
"_id" : ObjectId("5645f0443f32df13e0dc786e"),
"Booked Date" : ["28-9-2015","29-9-2015","1-10-2015"],
"Room No.": "101"
},
{
"_id" : ObjectId("5645f0c73f32df062064a0f6"),
"Booked Date" : ["29-9-2015","1-10-2015"],
"Room No.": "102"
},
{
"_id" : ObjectId("5645f0c73f32df06205874f8"),
"Booked Date" : ["29-9-2015","1-10-2015","2-10-2015"],
"Room No.": "103"
},
I do the query like this
db.booking.find({"Date": { '$nin':[Date]}},{"_id": False,"Booked Date": False})
Here I give the input as ["1-10-2015","2-10-2015"]
So when I get the result I don't know on which date the room is available.
Is there a way I can solve this problem.
When I run this code
db.booking.find({"Date": { '$nin':["1-10-2015","2-10-2015"]}},{"_id": False,"Booked Date": False})
I get the result
{"Room No.": "101"},
{"Room No.": "102"}
But I dont know for which date the room is available. In the above case it is available only on 2-10-2015.
So is there a way to say that it is available on 2-10-2015.
And for another case:
db.booking.find({"Date": { '$nin':["1-10-2015","2-10-2015", "3-10-2015"]}},{"_id": False,"Booked Date": False})
Here it is available on 2-10-2015 & 3-10-2015, but it should show only the first availability date i.e 2-10-2015.
The output should be the same as above:
{"Room No.": "101"},
{"Room No.": "102"}
"Room No": 103? what should the result be?3-10-2015? right?