How to select a single column and group array - mongodb

I want to select a single column in mgo which is array of strings currently using this returns a empty array
classifiedColl.Find(nil).Distinct("Tags", &tags)
and i want to group it how do i do it. The value i want to group is array of strings.

Related

Error when filtering some data with like and jsonb in PostgreSQL

I keep having a problem when filtering some data in postgresql.
For example, I want to filter by a json.
My jsons are saved in the following way
"[{\"Brand\":\"Leebo\"},{\"Housing Color\":\"Black\"},{\"Beam Type\":\"High Beam, Low Beam\"}]"
And let's say that I want to filter after
[{\"Brand\":\"Leebo\"}]
Shouldn't I write something like that in the query?
SELECT * FROM public.products
WHERE attributes is not NULL
AND attributes::text LIKE '%{\"Brand\":\"Leebo\"}%';
I tried also
SELECT * FROM public.products WHERE attributes::jsonb #> '"[{\"Material\":\"Artificial Leather\"}]"'
Because I won't receive data
Do you know how I could proceed differently?
But it only works if the column has all the data (eg if I give the exact data that is in the column)
Also, how could I search with whereIn?
You have an array in your JSONB because those characters ([ and ]) are array characters. If you are sure that you will always have only one array in your JSONB so you can use this:
SELECT * FROM public.products
WHERE attributes is not NULL
AND attributes[0]->>'Brand' = 'Leebo'
But if you can have several arrays inside JSONB then use jsonb_array_elements for extracting array elements, after then you can use JSONB operations like as ->>

Query a Map containing a string saved in Cloud Firestore with Dart/Flutter

I've a problem with a query condition. I would like to query a string inside an array that's also inside a map. I manage to receive the information complet without the where condition but when I do this condition I've an error.
My function to retrieve all information
My array with a map inside
If category is the only value inside the produits array items, you can check for its existence with array-contains:
collectionRef.where('produits', arrayContains({ 'categorie': 'Alimentation' }))
If there are more properties for the array item, you will need to specify all of them in the call to arrayContains. There is no way to query for a subset of the array item.
In such cases, a common workaround is to add a extra array field to the document with just the property values you want to query for. For example:
produitsCategories: ['Alimentation', '...']
With that field in place you can then query it with:
collectionRef.where('produitsCategories', arrayContains('Alimentation'))

PostgreSQL: array in text to array

I have JSON array in text type field in DB, and I want to check whether there are some elements in the array, and in that case, add some elements to this array and save it back to DB. The main issue I am having is that this text field looks like this:
["elem1","elem2","elem3"]
and I cannot figure out how to work with those double-quotes.
When I tried to_json it resulted in:
"[\"elem1\",\"elem2\",\"elem3\"]"
When I tried string_to_array:
{"[\"elem1\"","\"elem2\"","\"elem3\"]"}
I just need something like that:
['elem1', 'elem2', 'elem3']
To get a JSON array, simply cast it into json directly:
demo:db<>fiddle
SELECT '["elem1","elem2","elem3"]'::json
If you need a simple array (without JSON), you need to expand the elements of the JSON array (json_array_elements_text()) and aggregate them afterwards (array_agg()):
SELECT
array_agg(elems)
FROM mytable,
json_array_elements_text(mydata::json) AS elems

Order a SELECT by the first element of an array?

I have an array of strings as one of my columns, and I want to sort the result by the first element of the array. This is what I have tried:
SELECT * FROM items ORDER BY aliases[0];
This did not work. How may this be accomplished?
Arrays in Postgres are indexed beginning at position 1, not 0. From the documentation:
By default PostgreSQL uses a one-based numbering convention for arrays, that is, an array of n elements starts with array[1] and ends with array[n].
With this in mind, try the following query:
SELECT * FROM items ORDER BY aliases[1];

I want to get distinct and selected column names in mongodb

I want to get distinct of coulmn1 and the output should be selected column names(column1, column5) in mongodb