how to select PostgreSQL jsonb items? - php

I have a JSONB value that's
table={
"id": 1,
"items": [
{
"model": 1
},
{
"model": 2
}
]
}
how to select multiple items from that?
I've already tried
table[0]['items'][0];
but this selected only one object.

Try next:
SELECT '{"id":1,"items":[{"model":1},{"model":2}]}'::jsonb->'items'->0;
Test here

Related

My question is about mongodb do a query between multiple collection in PHP

I'm trying to use mongodb to do a query link SQL join table in PHP
Two collection are like following
Courses:
{
"CourseID": "CS101",
"Title": "Introduction to Data Science",
"Level": 6
},
{
"CourseID": "CS102",
"Title": "Application Design and Development",
"Level": 6
},
Offer:
{
"DeptID": "CS",
"CourseID": "CS101",
"Year": 2016,
"ClassSize": 40,
"AvailablePlaces": 40
},
I'm trying to make the result like the following:
prefering result here
MongoDB is not a relational database, but you can perform a left outer join by using the $lookup stage.
The $lookup stage lets you specify which collection you want to join with the current collection, and which fields that should match. Here's the official doc.
dbo.collection('courses').aggregate([
{ $lookup:
{
from: 'offer',
localField: 'CourseID',
foreignField: 'CourseID',
as: 'availableplaces'
}
}
])

Get data from database with WHERE clause from JSON Array in mySQL or Laravel

I have a column ordersdetail with type JSON in my mysql table orderAnalysis with below example.
first row
{ "id": 123, "items": [ { "id": 1623364501551, "product_id": 11931258628 }, { "id": 1623364534319, "product_id": 11931258630 }] }
second row
{ "id": 124, "items": [ { "id": 1623364501552, "product_id": 11931258629 }, { "id": 1623364534320, "product_id": 11931258632 }] }
I want to get row data if any product_id matched with given id from array of items. I have done R&D for it and got some solutions and when I tried that solutions none of thems didn't worked in my case.
I am tried using these queries as below:-
#first query
SELECT ordersdetail FROM `orderAnalysis` WHERE JSON_SEARCH(ordersdetail->>'$.items[*].product_id', 'one', 11931258628)
#second query
SELECT ordersdetail FROM `orderAnalysis` WHERE ordersdetail->"$.items[*].product_id" = 11931258628
#third query
SELECT ordersdetail FROM `orderAnalysis` WHERE json_extract(ordersdetail, '$.items[*].product_id') = 11931258628
But these queries not helped me to get result. I want first row as output in result. Please help me.
Thanx in advance

Cakephp 3: contain() function result a nested data

Problem:
I want to run this query in cakephp 3
SELECT
medicine_records.*,
medicines.name AS medicine_name,
medicines.subname AS `medicine_subname`
FROM medicine_records
INNER JOIN medicines ON medicines.id = medicine_records.medicine_id
Controller:
$medicines = $this->MedicineRecords->find()->contain(['Medicines'])->all();
Cakephp Queries log:
2017-02-14 09:58:47 Debug: duration=1 rows=1 SELECT MedicineRecords.id AS `MedicineRecords__id`, MedicineRecords.user_id AS `MedicineRecords__user_id`, MedicineRecords.medicine_id AS `MedicineRecords__medicine_id`, MedicineRecords.times_pre_day AS `MedicineRecords__times_pre_day`, MedicineRecords.created AS `MedicineRecords__created`, MedicineRecords.modified AS `MedicineRecords__modified`, Medicines.id AS `Medicines__id`, Medicines.name AS `Medicines__name`, Medicines.subname AS `Medicines__subname`, Medicines.type AS `Medicines__type`, Medicines.created AS `Medicines__created`, Medicines.modified AS `Medicines__modified` FROM medicine_records MedicineRecords INNER JOIN medicines Medicines ON Medicines.id = (MedicineRecords.medicine_id)
Result data:
"data": [
{
"id": 1,
"user_id": 1,
"medicine_id": 3,
"times_pre_day": 2,
"created": "2017-02-14T04:55:48+00:00",
"modified": "2017-02-14T04:55:48+00:00",
"medicine": {
"id": 3,
"name": "Name",
"subname": "Subname",
"type": 1,
"created": "2017-02-13T09:38:48+00:00",
"modified": "2017-02-13T09:38:48+00:00"
}
}
]
This result has medicine in child level. I want all data in top level like this:
"data": [
{
"id": 1,
"user_id": 1,
"medicine_id": 3,
"times_pre_day": 2,
"medicine_name": "Name",
"medicine_subname": "Subname",
"created": "2017-02-14T04:55:48+00:00",
"modified": "2017-02-14T04:55:48+00:00",
}
]
I already try to use public $recursive = -1; but I think it not working on Cakephp 3.
Update
Remove WHERE medicine_records.id = 1
Obviously that is the expected result,contain come in nested form and it should come this way.
No worries you can select fields within contain as you need:
$medicines = $this->MedicineRecords->find()
->contain(['Medicines' => function ($q) {
return $q
->select(['medicine_name'=>'name','medicine_subname'=>'subname']);
}])->all();
See Here (select within contain CakePHP 3).

How to filter distinct data in CloudSearch:AWS?

I have a query in aws cloudsearch. I did the following things
1) Created domain
2) uploaded the data & created indexing
I have data fields like : user_id, user_name, user_details, etc
My objective is to get the grouped/distinct data of particular field & its total count. In Cloudsearch Group by / Distinct key words not supported. So, I went through the cloudsearch documentation & done it by adding facet.user_id={} in my query string.
But I need user_name field data along with user_id and count.** Please update me regarding this.
Here is my full query : ?q="Tamil Selvan"&facet.user_id={}
Here is my query result :
{
"status": {
"rid": "isTcmOYp+AEKhpbc",
"time-ms": 6
},
"hits": {
"found": 986,
"start": 0,
"hit": []
},
"facets": {
"user_id": {
"buckets": [{
"value": "5",
"count": 213
}, {
"value": "182",
"count": 197
}]
}
}
}
My expected result :
{
"status": {
"rid": "isTcmOYp+AEKhpbc",
"time-ms": 6
},
"hits": {
"found": 986,
"start": 0,
"hit": []
},
"facets": {
"user_id": {
"buckets": [{
"value": "5",
"user_name":"Tamil Selvan",
"count": 213
}, {
"value": "182",
"user_name":"Tamil Selvi",
"count": 197
}]
}
}
}
The proper solution would be to look up the user_names for the user_id facet values from your datastore (which CloudSearch is not, or at least should not be).
CloudSearch is a search solution; you shouldn't be trying to ask it which user_name belongs to some user_id, as that's a question for your data store.

Laravel usage of GroupBy

I'm trying to group data in a table by cat_id and convert the result into JSON.
Table structure
id name cat_id
1 A 2
2 B 1
3 C 2
4 D 2
5 E 3
6 F 2
7 G 1
Here is the code I tried
$posts = Posts::groupBy('cat_id')->get();
return $posts->toJson();
Result should look like this:
[
{
"id": "1", //cat_id
"posts": [
{
"id": "2", //user id
"s": "B" //name
},
{
"id": "7",
"s": "G"
}
]
},
{
"id": "2", //cat id
"posts": [
{
"id": "1",
"name": "A"
},
{
"id": "3",
"name": "C"
},
{
"id": "4",
"s": "D"
},
{
"id": "6",
"s": "F"
}
]
}
]
But I'm getting Call to a member function toJson() on a non-object. Where am I doing wrong?
Unfortunately groupBy doesn't work like this. It's more like SQLs GROUP BY (actually that's what it does in the background...)
I assume your error comes from the fact that it doesn't know what to do with name and id because you always need aggregate fields with GROUP BY.
Now here's how you can achieve your goal. I assume you have set up the relation between Category and Post. somewhat like this:
class Category extends Eloquent {
public function posts(){
return $this->hasMany('Post', 'cat_id');
}
}
Then you are able to do:
$categories = Category::with('posts')->get();
And you get all the categories containing its posts. toJson() should work without a problem as well.
#lukasgeiter suggested right thing to do, however there's another way to go too. Depending on your needs you can choose the one for you:
$posts = Pots::all(); // fetch from the DB
$groupedByCat = $posts->groupBy('cat_id'); // call group by on the collection
Result will look like this:
{
"1": // cat_id
[
{
"id": "5" // post data
...
},
...
],
"2":
...
}

Categories