RethinkDB indexing two fields with time - javascript

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.

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

JavaScript: How do I sort times in a nested array?

I'm working with a JSON file that contains the 2021 NFL schedule. I'm able to easily filter out Thursday, Sunday & Monday games. The filtering method returns my Sunday games in an array of objects.
Below is a look at how my Sunday array is set up. This code just shows the first two weeks for the Packers, but there are 18 weeks in the schedule, and I have the same info for each team.
So for the first week, for example, I'll have an array of 14 games. (The 14 teams are the visiting teams.) I need to loop through the games for the desired week and sort the array by the time of the games. A typical Sunday will have games at 12:00 PM, 3:25 PM, and 7:15 PM. There are other starting times throughout the season.
[
{
"name": "Green Bay Packers",
"id": 1,
"nickname": "Packers",
"homefield": "Lambeau Field",
"division": "NFC North",
"schedule": [
{
"week": "1",
"date": "Sept. 12",
"weekday": "Sunday",
"time": "3:25 PM",
"network": "Fox",
"opponent": "New Orleans Saints",
"homeAway": "away",
"venue": "Mercedes-Benz Superdome"
},
{
"week": "2",
"date": "Sept. 19",
"weekday": "Monday",
"time": "7:15 PM",
"network": "ESPN",
"opponent": "Detroit Lions",
"homeAway": "home",
"venue": "Lambeau Field"
},
etc.
Figured it out. "su" is my array of Sunday games, so I pass that and the week number into my function.
function sortSundayByTime(su, weekNo) {
su.sort((a, b) => {
if (a.schedule[weekNo].time < b.schedule[weekNo].time) {
return -1;
} else if (a.schedule[weekNo].time > b.schedule[weekNo].time) {
return 1;
}
});
}

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

Accessing an specific atribute pair in a json object

I need to access temperature and append it to my page If these are all objects then I suppose I cant use an index since the order doesnt matter, which is throwing me off.
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
},
"current_observation": {
"image": {
"url": "http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
"title": "Weather Underground",
"link": "http://www.wunderground.com"
},
"display_location": {
"full": "Toms River, NJ",
"city": "Toms River"
},
"observation_location": {
"full": "Stonehedge, Toms River, New Jersey",
"city": "Stonehedge, Toms River",
"state": "New Jersey",
"country": "US"
},
"estimated": {},
"station_id": "KNJTOMSR5",
"observation_time": "Last Updated on April 11, 10:56 PM EDT",
"observation_time_rfc822": "Fri, 11 Apr 2014 22:56:39 -0400",
"observation_epoch": "1397271399",
"local_time_rfc822": "Fri, 11 Apr 2014 22:56:40 -0400",
"local_epoch": "1397271400",
"local_tz_short": "EDT",
"local_tz_long": "America/New_York",
"local_tz_offset": "-0400",
"weather": "Clear",
**"temperature_string": "59.6 F (15.3 C)"**
}
}
My javascript right now is plainly this since I havent figured out how to access this item yet
$(function() {
$("#getzip").submit(function() {
var zip_data =$(this).serialize();
$.getJSON("get_weather.php",null, function(data); {
I need to append this to the dom of my page, I think it shouldlook something like this?
(#output).append(data.temperature_string);
To get json object into javascript.
Use like this.
In Php
$json ='{"temperature":"36 c"}';
In Javascript
$.post("get_weather.php",null, function(data){
var temperature = data.temperature; // here you'll get temperature from json into variable
console.log(temperature);
},"json");
That's all
console.log(data["temperature_string"]) should access to that data.

Categories

Resources