I'm running the code:
DowntimeHistoryMonitoring::whereDate('created_at', '<=', now())->get(['id', 'created_at'])->groupBy(function ($item){
return $item->created_at->format('Y-m-d');
});
Which returns:
{
"2022-11-30": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T13:06:42.000000Z"
},
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T14:06:42.000000Z"
}
],
"2022-11-29": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-29T13:06:42.000000Z"
}
]
}
How can I do so that I get an empty result if I don't find records, and defined by a limit within the groupBy. Example with a limit of 5:
{
"2022-11-30": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T13:06:42.000000Z"
},
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T14:06:42.000000Z"
}
],
"2022-11-29": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-29T13:06:42.000000Z"
}
],
"2022-11-28": [
],
"2022-11-27": [
],
"2022-11-26": [
],
}
What should I do?
Related
I am new to the mongoDB aggregation and I have this situation. I have this Json and I need to convert by "select" this object:
{
"type": "PF",
"code": 12345
"Name": Darth Vader,
"currency": "BRL",
"status": "SINGLE",
"adress": [
{
"localization": "DEATH STAR",
"createDate": 1627990848665
},
{
"localization": "TATOOINE",
"createDate": 1627990555665
},
]
}
this way:
{
"type": "PF",
"code": 12345
"Name": Darth Vader,
"currency": "BRL",
"status": "SINGLE",
"localization": "DEATH STAR",
"createDate": 1627990848665
},
{
"type": "PF",
"code": 12345
"Name": Darth Vader,
"currency": "BRL",
"status": "SINGLE",
"localization": "TATOOINE",
"createDate": 1627990555665
}
So, after my query is complete, I will have 02 objects instead of 01. How can I do this?
I would like to do this via select because after converting I will sort by createDate and limit the number of records to return to the API. I'm using Criteria em my project.
The way to do this is $unwind, this will make 1 copy of the document, for each member of the array.
Test code here
db.collection.aggregate([
{
"$unwind": {
"path": "$user.adress"
}
},
{
"$set": {
"user": {
"$mergeObjects": [
"$user",
"$user.adress"
]
}
}
},
{
"$unset": [
"user.adress"
]
},
{
"$sort": {
"createDate": 1
}
},
{
"$limit": 10
}
])
Edit1 (the above is if user is a field, if it was the name of the collection)
$$ROOT is a system variable that has as value all the document
Test code here
Query
db.collection.aggregate([
{
"$unwind": {
"path": "$adress"
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
"$$ROOT",
"$adress"
]
}
}
},
{
"$unset": [
"adress"
]
},
{
"$sort": {
"createDate": 1
}
},
{
"$limit": 10
}
])
https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/TopLevel/batchRunReports
Request :
{
"entity": {
"propertyId": "XXXXXXXX"
},
"requests": [
{
"entity": {
"propertyId": "XXXXXXXX"
},
"dimensions": [
{
"name": "date"
},
{
"name": "dateHour"
},
{
"name": "firstUserCampaignName"
}
],
"metrics": [
{
"name": "sessions"
}
],
"dateRanges": [
{
"startDate": "2021-04-06",
"endDate": "2021-04-07"
}
],
"metricAggregations": [
"TOTAL"
],
"dimensionFilter": {
"andGroup": {
"expressions": [
{
"filter": {
"fieldName": "medium",
"stringFilter": {
"matchType": "EXACT",
"value": "Test"
}
}
}
]
}
},
"orderBys": [
{
"desc": true,
"metric": {
"metricName": "sessions"
}
},
{
"desc": false,
"dimension": {
"dimensionName": "dateHour"
}
}
],
"keepEmptyRows": true
}
]
}
Response:
{
"error": {
"code": 500,
"message": "Internal error encountered.",
"status": "INTERNAL"
}
}
But if remove following property from request:
"metricAggregations": [
"TOTAL"
],
I am able to see following response where there is not rows :
{
"reports": [
{
"metricHeaders": [
{
"name": "sessions",
"type": "TYPE_INTEGER"
}
],
"metadata": {},
"dimensionHeaders": [
{
"name": "date"
},
{
"name": "dateHour"
},
{
"name": "firstUserCampaignName"
}
],
"kind": "analyticsData#runReport"
}
],
"kind": "analyticsData#batchRunReports"
}
Any idea how to prevent 500 internal server error in this case ?
This error block google API call for an hour.
Furqan, there seems to be an issue with the Data API where a call using metricAggregations is failing in case the generated report is empty. In the meantime, to workaround this error, you can modify a query so that the resulting report contains more than 0 rows.
Question : How to retrieve Json data type data in SQL using a where condition in LARAVEL?
I want to display all the order that contains order->Product->user->id === 1
{
"currentUserID": 1,
"currentUserName": "Mohamed Naalir",
"order": [
{
"id": 26,
"Product": [
{
"id": 4,
"name": "Araliya Rice",
"desription": "Araliya Rice",
"salePrice": 500,
"category": "Rice",
"user": {
"id": 1,
"name": "Mohamed Naalir",
}
}
],
},
{
"id": 27,
"Product": [
{
"id": 2,
"name": "white sugar",
"desription": "aaa",
"salePrice": 100,
"category": "Sugar",
"user": {
"id": 5,
"name": "Mohamed Sharaf",
}
}
],
}
]
}
json where clauses
$orders = DB::table('orders')
->whereJsonContains('Product', [['user' => ['id' => 1]]])
->get();
I have document as below:
{
"_index": "abc_local",
"_type": "users",
"_id": "1",
"_version": 5,
"found": true,
"_source": {
"firstname": "simer",
"lastname": "kaur",
"gender": "1",
"Address": "Punjab House Fed. Housing Society, Amritsar, Punjab, India",
"email": "rav#yopmail.com",
"occupation": "Php Developer",
"work": "Development",
"fav_hunting_land": 2,
"zipcode": "",
"marital_status": "1",
"phone": "1234567899",
"school": "sdfergdfh",
"species": [{
"id": 1
}, {
"id": 2
}, {
"id": 3
}, {
"id": 4
}, {
"id": 5
}, {
"id": 6
}],
"activities": [{
"id": 1
}],
"fav_weapon": 6,
"weapons": [{
"id": 1
}, {
"id": 2
}, {
"id": 3
}, {
"id": 6
}],
"properties": [{
"id": 4
}]
}
}
and I need to match user on basis of weapons and I am trying something like:
$params = [
'index' => Constants::INDEX,
'type' => Constants::DOC_TYPE_USERS,
'body' => [
"query"=> [
"bool"=> [
"must"=> [ "match"=> [ "weapons.id"=>$params['weapons'] ]],
"should"=> [
[ "match"=> [ "firstname"=> $params['search_text'] ]],
[ "match"=> [ "lastname"=> $params['search_text'] ]]
]
]
]
]
];
as I am using elastic in PHP. Here $params['weapons'] is:
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
I get an error:
illegal_state_exception: Can't get text on a START_ARRAY at 1:36
Any suggestions/help would be appreciated that how I can match array. I took reference from nested datatypes
Update#1:
parameters I am sending to my function: {"from":0,"size":null,"city":null,"state":"0","weapons":["1","2"],"activities":[],"species":[],"properties":[],"search_text":"lastname"}
update#2:
Body of my query in json format:
{
"index": "abc_local",
"type": "users",
"body": {
"query": {
"bool": {
"must": {
"match": {
"weapons.id": ["1", "2"]
}
},
"should": [{
"match": {
"firstname": "simer"
}
}, {
"match": {
"lastname": "simer"
}
}]
}
}
}
}
You can simply replace the first match query by a terms one as match doesn't work with arrays of values.
$params = [
'index' => Constants::INDEX,
'type' => Constants::DOC_TYPE_USERS,
'body' => [
"query"=> [
"bool"=> [
"must"=> [ "terms"=> [ "weapons.id"=>$params['weapons'] ]],
^
|
change this
"should"=> [
[ "match"=> [ "firstname"=> $params['search_text'] ]],
[ "match"=> [ "lastname"=> $params['search_text'] ]]
]
]
]
]
];
if u want to check if any value from array matches to field from index then you have to "terms" instead of match.
{
"index": "abc_local",
"type": "users",
"body": {
"query": {
"bool": {
"must": {
"terms": {
"weapons.id": ["1", "2"]
}
},
"should": [{
"match": {
"firstname": "simer"
}
}, {
"match": {
"lastname": "simer"
}
}]
}
}
}
}
refer "Terms Level Query" in ElasticSearch docs.
Let's say I have two JSON objects (call them 'websites' and 'links'). I need to end up with both objects in a single CSV (ideally in separate columns).
I'm working with something like this:
File.open("file.json", "w") do |f|
combined = [websites, links]
f.write(JSON.pretty_generate(combined))
end
And then I'm using the Ruby gem json2csv to convert this file to a CSV. But when I do, I get the following error:
error: undefined method 'keys' for #<Array:0x007fea8a8e33f8>
I can't figure out what's wrong. When I look in file.json, it appears to be structured like this: [{websites}, {links}]. From my limited knowledge of JSON, I think that's right, but I could easily be wrong.
Also, I know this won't get me separate columns in the CSV. If anyone has an answer for that part, major bonus points.
EDIT: JSON examples included below; error message changed after minor fix on my end.
websites:
{
"uri": "https://v1/websites",
"id": 28235674,
"background": null,
"createdDate": 1399585684000,
"lastActivityDate": 1430682494000,
"lastCommunicationDate": 1430682494000,
"lastNonCommunicationChronicleDate": 1430330886000,
"lastModifiedDate": 1449263116000,
"lastViewedDate": 1421429034000,
"preferredContactType": null,
"rss": "",
"emailAddresses": [
{
"email": "",
"type": "Work"
},
{
"email": "",
"type": "Work"
},
{
"email": "",
"type": "Work"
},
{
"email": "not found",
"type": "Work"
}
],
"phoneNumbers": [
],
"streetAddresses": [
],
"socialNetworks": [
{
"profileUrl": "http://twitter.com",
"name": "Twitter"
},
{
"profileUrl": "http://www.facebook.com",
"name": "Facebook"
},
{
"profileUrl": "http://plus.google.com",
"name": "GooglePlus"
},
{
"profileUrl": "http://www.linkedin.com",
"name": "LinkedIn"
},
{
"profileUrl": "http://twitter.com",
"name": "Twitter"
}
],
"contactUrls": [
],
"tags": [
"tag1",
"tag2"
],
"mostRecentActivity": "https://v1/history",
"mostRecentChronicle": "https://v1/history",
"mostRecentCommunication": "https://v1/history",
"mostRecentNonCommunicationChronicle": "https://v1/history",
"projectStates": "https://v1/websites",
"history": "https://v1/history",
"customFieldValues": [
],
"name": "",
"primaryDomain": "",
"domains": [
""
],
"associatedPeople": "https://v1/people",
"payments": "https://payments",
"links": "https://v1/links",
"type": "https://v1/websites"
}
links:
{
"uri": "https://v1/links/custom_fields",
"id": 15529329,
"value": "Name",
"backgroundColor": null,
"customField": "https://v1/links/custom_fields"
}
combined output:
[
{
"uri": "https://v1/websites",
"id": 28235674,
"background": null,
"createdDate": 1399585684000,
"lastActivityDate": 1430682494000,
"lastCommunicationDate": 1430682494000,
"lastNonCommunicationChronicleDate": 1430330886000,
"lastModifiedDate": 1449263116000,
"lastViewedDate": 1421429034000,
"preferredContactType": null,
"rss": "",
"emailAddresses": [
{
"email": "",
"type": "Work"
},
{
"email": "",
"type": "Work"
},
{
"email": "",
"type": "Work"
},
{
"email": "not found",
"type": "Work"
}
],
"phoneNumbers": [
],
"streetAddresses": [
],
"socialNetworks": [
{
"profileUrl": "http://twitter.com/",
"name": "Twitter"
},
{
"profileUrl": "http://www.facebook.com",
"name": "Facebook"
},
{
"profileUrl": "http://plus.google.com",
"name": "GooglePlus"
},
{
"profileUrl": "http://www.linkedin.com/",
"name": "LinkedIn"
},
{
"profileUrl": "http://twitter.com/",
"name": "Twitter"
}
],
"contactUrls": [
],
"tags": [
"tag1",
"tag2"
],
"mostRecentActivity": "https://v1/history/",
"mostRecentChronicle": "https://v1/history/",
"mostRecentCommunication": "https://v1/history/",
"mostRecentNonCommunicationChronicle": "https://v1/history/",
"projectStates": "https://v1/websites/",
"history": "https://v1/history",
"customFieldValues": [
],
"name": "",
"primaryDomain": "",
"domains": [
""
],
"associatedPeople": "https://v1/people",
"links": "https://v1/links",
"type": "https://v1/websites"
},
{
"uri": "https://links/custom_fields",
"id": 15529329,
"value": "Name",
"backgroundColor": null,
"customField": "https://links/custom_fields"
}
]
JSON.pretty_generate() is expecting a hash and you are passing an array of 2 hashes. Start with: combined.map { |c| f.write(JSON.pretty_generate(c)) } and then mapping them into your CSV should be as easy as following the CSV documentation.