I have many to many relational data model. For example , i have a chat room and it has many users.
{Chatroom:
ChatRoomID:ID,
Somedata...
User:{
ID:userid,
Somedata...
}
}
I have a user object and i need to filtre those by user.
DataStore.query(ChatRoom,e=>e.User.Id("eq",myID))
I tried to query the data which has my userid then get the ChatRoomID. But i didnt work as expected. I can filter it with its main keys but i cant go through inside the object to filter. Currently i am doing this the same as the aws document. Which is-->
DataStore.query(ChatRoom).filter(e=> e.user.id==myID)
My concern is ,is this query above gets the whole data to filter ? For the first one, it looks like it getting the filtered data like select queries as we do in SQL. But i m worried about using second one. What if i have 100.000 rooms and 500.000 users inside it ? Is it get the whole data before filtering with a JavaScript functions like filter, map etc. ? I dont get the logic behind.
Related
i would like to know what is better:
The example is a shopping app and it is about the Main screen with a List of every Item available.
A) One document, for example "productList", with a Map in it where every Category, following another Map with every productId and a List with Data like the product Title, the showcase Image and the price.
Example code:
Stream stream = db.collection('products').document('productList').snapshots().asyncMap((list) => list.data);
or
B) many documents in a collection called "products" where the data is stored.
Example code:
Stream stream = db.collection('products').where('category', isEqualTo: "CategoryName").snapshots().map((list) => list.documents.map((doc) => doc.data));
(Both are working but with B i got with 3 products muuuuch more document reads wherefore i thought A would save a lot product reads but opening the question if there could be problems with that method)
Thank you very much :)
If you go with the first approach of trying to put everything into a single document, you will have scalability problems when the number of products becomes large. A Firestore document can only contain 1MB of data. After that, writes to it will fail, and you will have a hard time restructuring your app and its data in order to resolve the issue.
The only "right" way to store lots of items is to use one document for each item. The exception to this is when you have a hard guarantee about the number of items, and you are absolutely certain that the data for all those items can always fit into one document.
When trying to pull data from a MySQL database, I can do something like:
SELECT * FROM users ORDER BY id WHERE vehicle = car
That should get me all the users that drives a car and not show users that drives a motorcycle for instance.
Is there something like this for Firebase? I can only retrieve specific data from one user?
My firebase database is like this: user -> user info (name, age, vehicle etc..)
I want to query every user that drives a car and display them in a row. How do I do that?
I have tried the following, but I didn't succeed with what I tried to do, since after users the users id is the next child. Is there a way to query past that?
var recents = firebase.database().child('users').orderByChild('department').equalTo(department);
recents.on('child_added', function(snapshot) {
var countOfUserInDepartment = snapshot.count;
document.querySelector("#cphCount").innerHTML = countOfUserInDepartment;
});
There are no count queries (nor other aggregation queries) in the Firebase Database. Your options are:
Retrieve all data matching your query and count client-side
Keep a separate count-node that you update whenever you add/remove items.
For #2 you may find it convenient to use Cloud Functions, for which there an an example of keeping such a counter.
Also see:
Firebase count group by
Database-style Queries with Firebase
In Firebase, is there a way to get the number of children of a node without loading all the node data?
How to get size of an element/list in Firebase without get it all?
I'm having trouble trying to figure out how to get a list to query as desired. Full disclosure, it may not be possible and if that's the case then at least it will provide me with some closure. I want to query a friend's list based on presence, where friends who are active are queried on top.
Here is my database structure:
friendships
$UID
$UID: true
users
$UID
active:true/false
I need the list to stay synced so I need to use .on('value'). I've tried a couple different methods using child_added as well as iterating through a snapshot using snapshot.forEach() and pushing the childSnapshot key:value pairs into an array so I could then use lodash to order the list via the active key on the client side, but ran into the issue of it pushing a "new" item into the array for each active value change so there would be multiple items for any given user.
Any help or insight would be appreciated, spent the majority of yesterday attempting to figure this out before resorting back to an unorganized list using the following code:
const {currentUser} = firebase.auth();
firebase.database().ref(`/friendships/${currentUser.uid}`).on('value, snapshot => { //redux dispatch action, payload: snapshot.val() })
There's no great solution for this. Firebase doesn't have any sort of join or join query, so you could do usersRef.orderByChild('active').equalTo(true), but that will just return you all active users. You'll need to do separate queries to pull in their friendships.
I am working on a Trello sync bot based off of the GitHub one here : https://github.com/fiatjaf/trello-cardsync and I am working on the coffeescript files. I have looked on the API and I think I know what I need I just don't know how to write it exactly and I can't find any examples of it especially in coffeescript (or javascript).
I would like to be able to search through an organisations boards or a members boards and then pick out certain board IDs so I get just a list of board IDs to certain boards I want, so then I can create webhooks for these boards.
I've tried using Trello.get("members/me/boards", { fields: "id, name"}) to get all the boards IDs and names like in the client.js API 'Trello.get(path[, params], success, error)' but my GET just returns unidentified when I try to print it to console. I have an array of objects in the settings.coffee file to compare the names with to select the boards I want but I can't get the full list in my file. At the moment I'm just looking at the data from the link: https://trello.com/1/members/my/boards?key=substitutewithyourapplicationkey&token=substitutethispartwiththeauthorizationtokenthatyougotfromtheuserand entering the IDs manually to create the webhooks, but I would like to automate this as much as possible.
I have also looked at 'GET /1/search' in Trello API but I'm unsure how to do that in my coffeescript file.
You need to pass a callback to Trello.get. It doesn't return anything meaningful, but instead passes the value to the callback:
Trello.get("members/me/boards", { fields: "id,name"}, function(err, boards) {
console.log(boards); // got them!
console.log(err); // if something went wrong, this will be non-null
})
this is on mongodb
{cod_com:'WWWOAN',
cod_prod[{prod:'proda',info:'hola mundo'},{prod:'pacda',info:'hola mundo'},{prod:'prcdb',info:'hola mundo'}]
}
{cod_com:'WWWOA2',
cod_prod[{prod:'prdda',info:'hola mundo'},{ccod:'prcda',info:'hola mundo'}]
}
{cod_com:'WWWOA1',
cod_prod[{prod:'prcda',info:'hola mundo'},{aaad:'prcda',info:'hola mundo'}]
}
i have to get the info inside the cod_prod variables... so im working with two steps
first i query the owner of the cod_prod
db.collection.findOne({cod_com:'WWWOA1'});
if exists, i query the cod_prod that i need
db.collection.findOne({'cod_prod.prod':'prcda'});
so basically im querying two times, first is to the the owner of the product, and the second the info of the product that im searching...
the question is, there is any way to search the prod directly???
i cant use this...
db.collection.findOne({'cod_prod.prod':'prcda'});
because differents owners has same product... so first i get the owner and second i get the product....
anybody has a better aproach?? or a different way to get the info??? tnx
If you query like this:
db.collection.findOne({'cod_com':'WWOA1', 'cod_prod.prod':'prcda'});
you should get the result you want.