Filter search from json using jquery - javascript

I am building a onine bus booking web application like redbus.in in which i want to implement the filter search feature like the way it is implemented in redbus.
Here is the workflow
1.There are 4 dropdown boxes present with values droppint point, boardingpoint, bustype,opeartor name
2.when user select one dropping point from the dropdown the result will shows only with list of buses with that dropping point ,if he select dropping point and boarding point then it will show the list of buses that contains only two of this and so on.
Ie whatever the user select from these 4 dropdown menu the result (list of buses ) will contains only with these values.
But i don't know how to implement it especially the filtering .
Here is the sample json for one bus
{
"droppingPoints": [
{
"time": "09:45 PM",
"location": "Kachiguda",
"id": "1283518"
},
{
"time": "09:55 PM",
"location": "Narayanaguda",
"id": "1283519"
},
{
"time": "10:05 PM",
"location": "Lakdi ka pool",
"id": "1283520"
},
{
"time": "10:15 PM",
"location": "Punjagutta",
"id": "1283521"
},
{
"time": "10:25 PM",
"location": "Srinivasa Colony",
"id": "1283522"
},
{
"time": "10:25 PM",
"location": "SR Nagar",
"id": "1283523"
},
{
"time": "10:25 PM",
"location": "Ameerpet",
"id": "1283524"
},
{
"time": "10:30 PM",
"location": "Erragadda",
"id": "1283525"
},
{
"time": "10:35 PM",
"location": "Bharat Nagar",
"id": "1283526"
},
{
"time": "10:40 PM",
"location": "VIVEKANANDA NAGAR",
"id": "1283527"
},
{
"time": "10:40 PM",
"location": "Kukatpally",
"id": "1282840"
},
],
"availableSeats": 4,
"partialCancellationAllowed": false,
"arrivalTime": "07:00 AM",
"boardingPoints": [
{
"time": "10:45 PM",
"location": "K P H B",
"id": "1282841"
},
{
"time": "10:55 PM",
"location": "Hydernagar",
"id": "1283073"
},
{
"time": "10:55 PM",
"location": "Nizampet",
"id": "1283074"
},
{
"time": "11:00 PM",
"location": "MiyaPur",
"id": "1283072"
},
{
"time": "11:05 PM",
"location": "Miyapur Allwin X Road",
"id": "1283422"
},
{
"time": "11:15 PM",
"location": "Kondapur",
"id": "1283423"
},
{
"time": "11:20 PM",
"location": "Gachibowli",
"id": "1283554"
}
],
"operatorName": "Morning Star Travels",
"departureTime": "10:30 PM",
"mTicketAllowed": true,
"idProofRequired": true,
"serviceId": "1572",
"fare": "1090.0, 1190.0",
"busType": "2+1 Sleeper Non A/C",
"routeScheduleId": "84W157YI3YGZKH5B9K0TG4E40VI13",
"commPCT": 7.6,
"operatorId": 1572,
"inventoryType": 2
}
Currently i have this code in my hand
<script>
//when select item from dropping point
$('.drp').on('change',function(){
search_buses();
});
//when select item from boarding point
$('.brp').on('change',function(){
search_buses();
});
//when select item from butype
$('.bt').on('change',function(){
search_buses();
});
//when select item from opearto name
$('.on').on('change',function(){
search_buses();
});
//function for searching
function search_buses(){
//list of buses in json format.
var bus_list;
}
</script>
i Want to implement filter functionality in search_bus function. but i don't know how to do that.

Filtering would be as easy as using the filter and the some javascrip methods:
function searchBusesByLocation(location) {
filteredBuses = busesArray.filter(function (bus) {//this will create a new array with the filtered items
return boardingPointLocationComparisson(bus, location);
});
return filteredBuses;
}
function boardingPointLocationComparisson(bus, location) {//i'm guessin you want to filter by location, you can do by date or adapt this function like you want
return bus.boardingPoints.some(function (boardingPoint) {
//the some function will iterate over the array and return true whenever the comparisson is true, false otherwise
return boardingPoint.location === location;
})
}
Then you call the searchBusesByLocation on the change event with the location the user has entered (you can adapt the code for the other cases).

Related

Merge collection instantly and make a new collection with grouping

Currently using MongoDB Atlas for a Timesheet system. I have 4 collections in it:
category (has the record of clock in types and staff roles)
employees (has the record of the staff name and a unique pin)
timesheets (has the record of a Pin with the clock in type and date and time)
users (Admin login details)
Data of employees is as follows:
{
"_id": {
"$oid": "62cf98457"
},
"name": "Name Test",
"pin": "1234",
"role": "Sales",
"site": "Port Melbourne",
"email": "test#gmail.com",
"phone": "999 999 999",
"dob": "2022-07-10",
}
Data of timesheet is as follows:
{
"_id": {
"$oid": "62cf98e47caaa2ac70fba490"
},
"pin": "1234",
"type": "in",
"date": "18-01-2022",
"time": "Tuesday, January 18, 2022 6:25 AM",
"image": "image file url"
},{
"_id": {
"$oid": "62cf98e47caaa2ac70fba490"
},
"pin": "1234",
"type": "out",
"date": "18-01-2022",
"time": "Tuesday, January 18, 2022 4:25 PM",
"image": "image file url"
}
For now I am querying the employees collection if pin exits or not and if it does filtering in the timesheets collection which has same pin values again filtering them by date and then by types with time to get all employees timesheets details is a list with date.
This process is taking long for a query.
Can we directly create a new collections example employees_timesheets which will group user details and create an object inside filtering dates from timesheets collection.
Example:
{
"_id": {
"$oid": "62"
},
"name": "Name Test",
"pin": "1234",
"role": "Sales",
"date": "18-01-2022",
{
"18-01-2022": [{
"type": "in",
"time": "Tuesday, January 18, 2022 6:25 AM",
"image": "image file url"
},{
"type": "out",
"time": "Tuesday, January 18, 2022 4:25 AM",
"image": "image file url"
}]
}
}

MongoDB Aggregation - Field must be a accumulator object in group stage

I am new to mongodb aggregation. Let's say I have this data:
[
{
"end_year": 2020,
"intensity": 12,
"sector": "Information Technology",
"topic": "robot",
"insight": "62 market research reports study robotics industry",
"url": "http://robohub.org/62-market-research-reports-study-robotics-industry/",
"region": "",
"start_year": 2016,
"impact": "",
"added": "July, 16 2016 02:44:49",
"published": "July, 11 2016 00:00:00",
"country": "",
"relevance": 3,
"pestle": "Economic",
"source": "Robothub",
"title": "Forecasts the industrial robotics market in APAC to grow at a CAGR of 8.7% during the period 2016-2020 in the top 5 segments.",
"likelihood": 4
},
{
"end_year": "",
"intensity": 2,
"sector": "Government",
"topic": "government",
"insight": "Oil, Greed, and Grievances in the Middle East and North Africa",
"url": "https://www.newsecuritybeat.org/2016/07/oil-greed-grievances-middle-east-north-africa/",
"region": "",
"start_year": "",
"impact": "",
"added": "July, 16 2016 01:12:21",
"published": "July, 12 2016 00:00:00",
"country": "",
"relevance": 1,
"pestle": "Political",
"source": "New Security Beat",
"title": "Countries threatened by larger-scale insurgencies might want to consider giving regional groups some share in central government decision-making.",
"likelihood": 2
}
]
This is my query:
// return all the countries associated with a single topic
const data = await Data.aggregate([
{ $match: {} },
{ $group: { _id: "$topic",country: ["$country"],likelihood: ["$likelihood"],relevance: ["$relevance"],intensity: ["$intensity"] } }
])
I want to return all countries,likelihoods,relevances associated with a single/common topic
I know how to get the first and last ones using $first and $last operators. How do I get all of them in an array?
We can use $push operator to add all of the field values:
const data = await Data.aggregate([
{ $match: {} },
{ $group: { _id: "$topic",data: { $push: { country: "$country",likelihood: "$likelihood",relevance: "$relevance",intensity: "$intensity" } } } }
])

How to query data with and conditions in mongoose

I have a document of transactions where a transaction can be of different shops.
I want to query transactions with certain id in certain date. I have a field for date and id which is a MongoDB id, when I query using
Transaction.find({ $and: [{ date: nepali_date.day, shop_id: user.shop_id }] }).lean()
It returns data of all the transaction with the date given, I don't want that, I want the data of date 14 AND shop id "61f3c70c89e5453d271e0ea7"
but instead it returns:
{
"_id": "61f3c81a1410f50644d981b6",
"year": 2078,
"month": 9,
"date": 14,
"title": "Krus",
"amount": 60,
"type": "income",
"author": "Eclipsu",
"authorID": "61ef7e9565a4a88874937e96",
"shopID": "61f3c70c89e5453d271e0ea7",
"edited": false
},
{
"_id": "61f3c89ce0ea4276ddba693f",
"year": 2078,
"month": 9,
"date": 14,
"title": "RAJ",
"amount": 50,
"type": "income",
"author": "Eclipsu",
"authorID": "61ef7e9565a4a88874937e96",
"shopID": "61ef7ea165a4a88874937e9d",
"edited": false
}```

RethinkDB indexing two fields with time

I have RethinkDB with data/table let say "news" with huge number of data row :
[
{
"author": "author1",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author2",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description2",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "sport",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description3",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description4",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
}
.....
]
I need to create index for category_id and created_at (timestamp) and query for certain category and filter by day now (certain date) only. I want to optimize and speed up for query result
I can do it in javascript by filter like this for category_id business and day 15 :
r.table("news").filter({category_id: 'business'}).filter(
r.row("created_at").day().eq(15)
)
But how I can create index for category_id and created_at and query it by certain day of created_at.
r.table("news").indexCreate( ???
thanks
Should be something like this:
r.table('news').indexCreate('businessAndDate', function (doc) {
return doc('category_id').add(doc('created_at').day().coerceTo('string'));
});
Then you can:
r.table('news').getAll('business15', {index: 'businessAndDate'})
You may want to insert a separator between the data (like 'business-15'), and use more than just the day (otherwise, why bother having a full timestamp?), that's up to you and how many .add you're willing to write. ;)
According to the reference, this is called an arbitrary index.

Matching for the Values in an array in javascript

I have bunch of objects in an array with format as below:
[
{
"card_details": {
"cardType": "gift"
},
"merchant_details": {
"merchantName": "Walter Heisenberg1",
"timeZone": "+05:30",
}
},
{
"card_details": {
"cardType": "coupon",
"cardTitle": "Coupon",
"messageUser": "Hi",
"punchCount": null,
"messageReachPunchLimit": "Get your Freebie!",
"merchantId": "59c214000e1a7825184cb813",
"expiryDate": "21 Sep 2019",
"discountPercent": "15",
"cardImageUrl": ""
},
"merchant_details": {
"merchantName": "Walter Heisenberg1",
"timeZone": "+05:30"
}
},
{
"card_details": {
"cardType": "punch",
"cardTitle": "BlueM2",
"messageUser": "Welcome!",
"expiryDate": "21 Sep 2019",
"punchCount": 25,
"messageReachPunchLimit": "Get your Freebie!",
"merchantId": "59c214000e1a7825184cb813",
"cardImageUrl": "http://139.59.179.111/cloopapi/undefined"
},
"merchant_details": {
"merchantName": "Walter Heisenberg1",
"timeZone": "+05:30"
}
}
]
I want to filter the objects based on cardType in the card_details object. But I want to search from the array. For example, if I search for ["coupon","gift"], then I should get the all the cards which have the card_details.cardType as coupon or gift.
I need to be able to do this in Node.js,i.e., Javascript.
You can use ES6 filter and includes functions here:
collection.filter(item => ['coupon', 'gift'].includes(item.card_details.cardType))

Categories

Resources