How to modify my object to order to show first a specific id my object looks like this
{ 1 : [{ id : 1 , name : 'toto'}],
9: [{ id : 9 , name : 'toto'}]
15 : [{ id : 15 , name : 'toto'}]
}
I want to show first 9 how can I do this
Related
I'm working on a web application for a farmer, a permacultor to be more precise. It implies that he's handling various seeds and plants. The purpose of the application is to store in database data regarding his garden. Once enough data are gathered, the application is to provide a frame to analyze data.
For now I'm developping the first fonctionality : store data in database and display them on webpages.
Let's focus on the main topic of this question.
The garden has several fields. A field can contain several plants. Plants can have several state throught time (seed, plant, flower, harvestable).
When a plant reach a state, we need to store specific information :
the state
the date when state was observed (and other time-related data)
the quantity (from seed state to flower state, we can have loss for many reasons)
So far so good, nothing fancy.
NOW a plant can grow on a field until a specitifc state then be planted into another field until its harvesting.
For instance, 12 carrots that are growing in tray n°3 from the seed to germination state.
At germination state, 2 carrots didn't make it. The farmer now intend to resume the growing of his carrots not in tray n°3 but in field n°1
In model, let's say "state_plant_table" you have 2 entries :
carrots - 12 - seeds - tray n°3
carrots - 10 - germ - field n°1
You might see it coming.
Let's say now that... there isn't enough room in field n°1 for the 10 carrots, only 8 can fit. So he just put the 2 left in the field aside - field n°2.
We now have
carrots - 12 - seeds - tray n°3
carrots - 8 - germ - field n°1
carrots - 2 - germ - field n°2
NOW, on display we would show HTML table for each fields, trays or w/e. When you click on a field you have the detail of every plants rooted in it.
For field n°1 we would have :
carrots - 8
For field n°2 we would have :
carrots - 2
And, unfortunately, for tray n°3, we would have :
carrots - 12
But we should have 0 (if 0 => exclude from display of course).
I'm struggling with the theorical design of my process right now... any tips, hints, suggestions are welcome !
I have thought about a "parent" quantity and a "child" quantity where the initial quantity would be store in "plant_table" as "parent" quantity and "children" quantity would be stored in "state_plant_table" - the quantity is more linked to a state in which it's being observed than the plant itself.
I feel like this is the right way, but I don't manage to push the reasoning to its end either.
Reasoning with "parent" and "children" was one of the correct approach.
There are actually 3 natures of quantity to store :
quantity of plants observed at a certain state (quantite_etat)
quantity of plants actually in the field (quantite)
quantity of plants from the same parent (quantite_lot)
models.py
class EtatLot(models.Model):
id = models.AutoField(primary_key = True)
id_lot = models.ForeignKey('Lot', on_delete = models.PROTECT, verbose_name = 'Lot', related_name = 'lot_parent', null = True, blank = True)
etat = models.CharField(verbose_name = 'État', max_length = 50, null = True, blank = True)
quantite = models.PositiveSmallIntegerField(verbose_name = 'Quantité', null = True, blank = True)
quantite_etat = models.PositiveSmallIntegerField(verbose_name = 'Quantité relatée', null = True, blank = True)
class Lot(models.Model):
id = models.AutoField(primary_key = True)
quantite_lot = models.PositiveSmallIntegerField(verbose_name = 'Quantité', null = True, blank = True)
The quantity displayed is the one from quantite. The quantity used to analyze data is the one from quantite_etat.
Example
Let's say we have 12 cauliflower on field n°1 and the farmer want to plant 10 of them on field n°2.
Within database we have :
Lot table
id : 1 quantite_lot : 12
EtatLot table
id : 1 id_lot : 1 etat : Seed quantite : 12 quantite_etat : 12
id : 2 id_lot : 1 etat : Germ quantite : 12 quantite_etat : 12
At the end of operations, we should have this :
Lot table
id : 1 quantite_lot : 2
EtatLot table
id : 1 id_lot : 1 etat : Seed quantite : 12 quantite_etat : 12 field : fieldn°1
id : 2 id_lot : 1 etat : Germ quantite : 2 quantite_etat : 12 field : fieldn°1
id : 3 id_lot : 1 etat : Plan quantite : 10 quantite_etat : 10 field : fieldn°2
For this operation, quantite_lot is irrelevant. However I store it in order to do some stock check : you cannot plant more plants that you have.
This is how I achieved to the table above :
get quantity from the last child of lot_parent (quantite)
update this value with the difference between it and the value added by the farmer in the form, update the value from quantite_lot in the parent as well
store the value of the form in quantite and quantite_etat of the entry that is about to be added in the database
Lets assume we have a PostgreSQL db with a table with rows of the following kind:
id | doc
---+-----------------
1 | JSON Object
2 | JSON Object
3 | JSON Object
...
The JSON has the following structure:
{
'header' : {
'info' : 'foo'},
'data' :
[{'a' : 1, 'b' : 123},
{'a' : 2, 'b' : 234},
{'a' : 1, 'b' : 543},
...
{'a' : 1, 'b' : 123},
{'a' : 4, 'b' : 452}]
}
with arbitrary values for 'a' and 'b' in 'data' in all rows of the table.
First question: how do I query for rows in the table where the following condition holds:
There exists a dictionary in the list/array with the key 'data', where a==i and b>j.
For example for i=1 and j=400 the condition would be fulfilled for the example above and the respective column would be returned.
Second question:
In my problem I have to deal with time series data in Json. Every measurement is represented by one Json and therefore one row in the table. I want to identify measurements where certain events occurred. For the case that the above structure is unsuitable in terms of easy querying: How could such a time series look like to be more easily queryable?
Thanks a lot!
I believe a query like this should answer your first question:
select distinct id, doc
from (
select id, doc, jsonb_array_elements(doc->'data') as elem
from docs
) as docelem
where (elem->>'a')::int = 4 and (elem->>'b')::int > 400
db<>fiddle here
I have a mongoDB document having certain columns like Id, EmployeeID, SiteID, EmployeeAddress.
A employee can be present at a site more than once.
I want to have a group by query along with count which will give result set as
EmployeeID SiteID Count EmployeeAddress
basically how many times an employee is present as a site.
I am using this query but not getting the desired data.
db.pnr_dashboard.aggregate(
[
{
"$group" : {
"_id" : { "siteId" : "$siteId" , "employeeId" : "$employeeId"} ,
"count" : { "$sum":1}},
}
]
);
I have solr document which is like below.
agentId : 100
emailDeliveredDate : 2018-02-08,
emailSentDate : 2018-02-07
agentId : 100
emailSentDate : 2018-02-06
agentId : 101
emailDeliveredDate : 2018-02-08,
emailSentDate : 2018-02-07
I need a result like below.
agentId : 100
emailDeliveredDate : 1,
emailSentDate : 2
agentId : 101
emailDeliveredDate : 1,
emailSentDate : 1
In mysql it will be :
select count(emailDeliveredDate),count(emailSentDate) group by agentId;
I need help in solr for this.
I did not get any way in Solr which can help me. So I used facet with pivot which gave me half results. Rest half calculation I did in Java.
I have a problem with a filter condition in BO.
Imagine that I have this database
ID | DESC
0 | None
1 | Company
2 | All
In BO I have a filter that ask where do you want to find the objects and 2 options:
"Company" or "All".
If I choose "All" then I should have all the datas with the "ID" 0,1,2 and if I choose "Company" only the data with the "ID" 1.
So I did something like this:
TABLE_NAME.ID <= (CASE WHEN #Prompt('where do you want to find the objects','A',{'Company', 'All'},mono,constrained,not_persistent,{'Company'}) = 'Company' THEN 1 ELSE 2 END)
This filter is OK when I choose "All" because I have all the "ID" smaller than 2, i.e, 0,1,2.
But It does not work when my option is company, because it also shows the data with the "ID" 0.
I should have some with "=" combined with "<="
If it's really only that simple, the following will work:
TABLE_NAME.ID =
(CASE #Prompt('where do you want to find the objects',
'A',
{'Company', 'All'},
mono,
constrained,
not_persistent,{'Company'}
)
WHEN 'Company'
THEN 1
WHEN 'All'
THEN TABLE_NAME.ID
END)