Firebase Fanout Structure List Query - javascript

For the Firebase fanout data structure example of users and groups, what would be the most efficient way to retrieve user/member ("users") detail data for a given group from the "groups" list? Let's say the goal was to display the "name" property for each member of group "techpioneers"?
{
"users": {
"alovelace": {
"name": "Ada Lovelace",
"groups": {
"techpioneers": true,
"womentechmakers": true
}
},
},
"groups": {
"techpioneers": {
"name": "Historical Tech Pioneers",
"members": {
"alovelace": true,
"ghopper": true
}
},
...
}
}
Would using a combination of orderByChild() and equalTo() be the best approach to find users who have "techpioneers" key and get at each of their data such as the "name" property? How would you access a property such as "name" once you've determined which users are part of the target group?
let usersRef = firebase.database().ref('users')
usersRef.orderByChild("groups").equalTo('techpioneers').on('child_added', (snapshot) => {
// how would you access user "alovelace" name property?
console.log(snapshot.val().name);
});
Thank you for any help you can provide.

Related

Elastic Search Json field query with number key

How can I query elastic search based on the number key?
JSON field name
years_of_experience :
"{\"61\": \"10\", \"8240\": \"5\", \"8249\": \"2\", \"50\": \"0\", \"2079\": \"2\"}"
I want to filter years_of_experience like 50:0.
So, according to your sample, you have documents like the below:
POST myindex/_doc
{
"years_of_experience": {
"50": "0",
"61": "10",
"2079": "2",
"8240": "5",
"8249": "2"
}
}
So, you have an object for years_of_experience, and you want to do an exact match with the field name and values. You need to set all fields inside this field you want to set as a keyword type. First, you need to handle the mapping part of this problem. Here is a solution for this :
PUT myindex
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"dynamic_templates": [
{
"strings_as_keyword": {
"match_mapping_type": "string",
"path_match": "years_of_experience.*",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"years_of_experience": {
"type": "object"
}
}
}
}
While creating your index for this data, you need to use a dynamic template for the years_of_experience object. And all the fields inside this will be keyword type, and you can run term queries on these fields.
So now we can create the documents after creating an index with the above settings. And you can filter the data as below :
GET myindex/_search
{
"query": {
"term": {
"years_of_experience.50": "0"
}
}
}

Modify Response Object in strapi

I would like to get a modified response object. For example I dont know how to get the user object without the roles.
The default response is:
{
"id": 6,
"username": "username",
"email": "user#email.com",
"provider": "local",
"confirmed": true,
"blocked": false,
"role": {
"id": 2,
"name": "Authenticated",
"description": "Default role given to authenticated user.",
"type": "authenticated"
}
}
Now I want to get the same response without the role attribute.
{
"id": 6,
"username": "username",
"email": "user#email.com",
"provider": "local",
"confirmed": true,
"blocked": false
}
Currently you cannot do this in the Rest API unless you change the UserController provided by permissions plugin, which is not recommended.
What you can do then is to use the GraphQL plugin provided by Strapi, so you can query only the fields you need on client side.
The docs about how to use GraphQL plugin are here.
For anyone still struggling with this problem:
The latest versions of strapi do support custom queries, you can pass an array containing all the names of relations you wish to populate (only relations!).
If you don't want to populate any relationships, you can keep it empty, your controller would then look something like this:
module.exports = {
UserWithoutRoles: ctx => {
return strapi.query('user').findOne({ id: ctx.params.id }, ['']);
}
}
If you do wish to populate it, it would be like this:
module.exports = {
UserWithoutRoles: ctx => {
return strapi.query('user').findOne({ id: ctx.params.id }, ['role']);
}
}
Also see:
[https://strapi.io/documentation/3.0.0-beta.x/concepts/queries.html#api-reference][1]

Storing JSON in Redis where several data items are optional

I need to store the data in Redis in the following structure.
{
"userId":1,
"latitude":44.24,
"longitude":-100.24,
"items": [
{
"name":"Rollerball Pen",
"attributes":[
{"Weight":"10 grams"},
{"Manufacturer":"Luxor"}
]
},
{
"name":"Measuring Tape"
}
]
},
{
"userId":2,
"items": [
{
"name":"Laptop",
"attributes":[
{"Brand":"DELL"}
]
},
{
"name":"Scissor"
}
]
},
{
"userId":3,
"latitude":47.24,
"longitude":-102.37
},
{
"userId":4
}
The key is the user id, probably like this:
"user:" + userId.toString()
Note that following are optional:
a. Latitude and Longitude
b. array of items
c. array of attributes (in items)
I have tried to sum up every possible use case using the 4 different user ids.
How can I store and access this kind of data in Redis, kindly help me out. The language that I am using is JavaScript.

DocumentDB: How to filter document on array within array?

Let's say I have the following document:
{
"Id": "1",
"Properties": [
{
"Name": "Name1",
"PropertyTypes": [
"Type1"
]
},
{
"Name": "Name2",
"PropertyTypes": [
"Type1",
"Type2",
"Type3"
]
}
]
}
When I use the following SQL:
SELECT c.Id FROM c
JOIN p in c.Properties
WHERE ARRAY_CONTAINS(p.PropertyTypes,"Type1")
I get as return:
[
{
"Id": "1"
},
{
"Id": "1"
}
]
How do I change my query so that it only returns distinct documents?
As far as I know, Distinct hasn't supported by Azure Cosmos DB yet.
It seems that there is no way to remove the repeat data in the query SQL level.
You could handle with your query result set in the loop locally.
However, if your result data is large,I suggest you using a stored procedure to handle with result data in Azure Cosmos DB to release the pressure on your local server.
You could refer to the official tutorial about SP.

Firebase querying - issue with indexing unique ids

My database structure looks like this (simplified):
{
"articles": {
"-uniqueId1": {
"title": "Article 1",
"category": "news"
},
"-uniqueId2": {
"title": "Article 2",
"category": "other"
},
"-uniqueId3": {
"title": "Article 3",
"category": "news"
},
"-uniqueId4": {
"title": "Article 4",
"category": "news"
}
},
"articlesByCategory": {
"news": {
"-uniqueId1": true,
"-uniqueId3": true,
"-uniqueId4": true
},
"other": {
"-uniqueId2": true
}
}
}
The query needs to fetch articles where a specific article's category isn't within. Does that make sense? Say, if uniqueId2 is of category "other", the query would only fetch articles within "news" and all other existing categories. But since the list may contain, say millions of articles, I have to be as specific as possible and not fetch articles that doesn't match this exact criteria. Therefor, a query like below would be ideal:
const ref = firebase.database().ref("articlesByCategory");
ref.orderByChild("-uniqueId2").equalTo(null).once("value", function(snapshot) {
console.log(snapshot.key);
});
Here I am searching for categories where a specific property is absent (where a property equals to null). This is explained here: Firebase: Query to exclude data based on a condition
However, doing a query like this requires me to index every article's unique id on "/articlesByCategory" in the security rules. But it would be unrealistic and non-optimal to dynamically add new article ids inside the ".indexOn" array as that would end up with millions of unique (auto-generated) ids:
{
"articles": {
".read": true,
".write": true
},
"articlesByCategory": {
".read": true,
".write": true,
".indexOn": ["-uniqueId1", "-uniqueId2", "-uniqueId3", "-uniqueId4"...] // List would be endless!
}
}
So how is this achievable? Why am I seeing these sorts of structures (inverted indexing) as ideal solutions everywhere on StackOverflow, but no one seems to tackle this issue?
Firebase only can query order statements on nodes with auto-generated ids, and when you've defined your own keys, firebase is unable to perform any sorting/ordering
The ideal way to generate keys is
firebase.getInstance().getReference("articles").push(myArticle);
this will result in a slightly different database structure like
{
"12dakj137_9": { // this is a auto-generated id
"article1": {
"title": "Article 1",
"category": "news"
},
"12asjh_123": {
"title": "Article 2",
"category": "other"
},...
}
Now firebase is able to order/sort and the way you do this is by
firebase.getInstance().child("articles").orderByChild("category").equalTo("other");

Categories

Resources