Referencing firebase document variables from another document - javascript

I'd like to have two separate database collections (users and public-users). To access/edit documents in the users collection, you need to be the owner of the document. However, for a public leaderboard, I want the displayName and uid of each user in the users collection to be available for everyone to read. To save me the hassle/chance of error, I'd like to reference the documents displayName property (from the users collection ) in the public-users collection.
Is something like this possible?

Firestore does not have any way to link the value of a field in a document to another specific field in other document. You have two options:
Duplicate the entire value between the fields in both users and public-users. You will likely need to keep all the copies up to date if you want any one of them to change.
Write client code to read the document from public-users that matches the document from users. This requires two read operations. There needs to be enough information in the public-users document to find the matching users document. It sounds like they could simply have the same document ID.
There is no way to make it any more simple than these options.

Related

Using Firebase Firestore in a web app, can i exclude certain fields from being returned?

I could not find anything in the docs, but say I perform a query such as
const snapshot = await jobsRef.where('status', '==', STATUS_ACTIVE).get();
Could exclude certain fields from being returned for this "job"?
There are certain fields in the "job" document that should remain private.
For web and mobile clients, it's not possible to exclude certain fields from a query. When you query a document, it will always deliver all of the fields for all of the matching documents. Security rules will not help you with this.
For cases where there are public and private fields to separate, you should make two different collections and protect them with different security rules. You can choose either two top-level collections:
/jobs-public/{id}
/jobs-private/{id}
Or you can use subcollections:
/jobs/{id}/public/{id}
/jobs/{id}/private/{id}
In either case, you will have to make sure that the user can only read the documents that they are allowed to by your requirements.
It's not possible to retrieve certain fields and as Doug mentioned above, you can separate the fields in different collection or sub-collection.
If you do not wish to redesign your database structure, you can alternatively use Firebase Cloud Functions in which you can fetch the required document, filter the data received and return the required fields to user. Hence user won't be able to see complete Document either.
Though this will obviously increase your costs as you will be using Cloud Functions.
Though it's really effective to way filter and Verify who is retrieving the data.

Firebase: Database + Storage - best practice of referencing the correct image of a post | Vue.js

Vue.JS
Vuex
firebase / firestore
I have created a website where you can submit text and an image for your post.
Currently I am giving the post.postPicture the entire URL of the image that belongs to it. That works.
The images are stored in the firebase storage under /$useruid/$filename.
Now, as it is prone to happen, what if the filename or userid changes? (assuming i don't fix the path)
What is the best practice of referencing the image that belongs to the post?
It's not a good idea to use data that changes in the path of a document. Avoid that.
It's not clear to me what your userid is exactly. User IDs issued by Firebase Authentication should never change. If you're using some sort of screen name that the user can change, that should not be used in the path. You should instead assign each user a unique, unchanging ID, and store their screen name in a separate document attached to that ID.
It's also not clear to me what exactly your filename is. But the same principle applies - if it can be changed, then it shouldn't be part of a document path. Generate a random ID and put the file name in a field in a document associated with that ID.
This sort of modeling with random IDs is the whole reason why operations like add() exist in Firestore. add() will generate that random ID for you, and you populate the document with data that helps you find it later. Read more in the documentation.

How to write data into a specific location Firebase Web

I want to write data into a specific location in the database. Let's say, I have a couple of users in the database. Each of them has their own personal information, including their e-mails. I want to find the user based on the e-mail, that's to say by using his e-mail (but I don't know exactly whose e-mail it is, but whoever it is do something with that user's information). To be more visible, here is my database sample.
Now, while working on one of my javascript files, when the user let's say name1 changes his name, I update my object in javascript and want to replace the whole object under ID "-LEp2F2fSDUt94SRU0cx". To cut short, I want to write this updated object in the path ("Users/-LEp2F2fSDUt94SRU0cx") without doing it by hand and just "knowing" the e-mail. So the logic is "Go find the user with the e-mail "name1#yahoo.com" and replace the whole object with his new updated object". I tried to use orderByChild("Email").equalTo("name1#yahoo.com").set(updated_object), but this syntax does not work I guess. Hopefully I could explain myself.
The first part is the query, that is separate from the post to update. This part is the query to get the value:
ref.child('users').orderByChild("Email").equalTo("name1#yahoo.com")
To update, you need to do something like this once you have the user id from the query result:
ref.child('users').child(userId).child("Email").update(newValue);
firebase.database.Query
A Query sorts and filters the data at a Database location so only a
subset of the child data is included. This can be used to order a
collection of data by some attribute (for example, height of
dinosaurs) as well as to restrict a large list of items (for example,
chat messages) down to a number suitable for synchronizing to the
client. Queries are created by chaining together one or more of the
filter methods defined here.
// Find all dinosaurs whose height is exactly 25 meters.
var ref = firebase.database().ref("dinosaurs");
ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) {
console.log(snapshot.key);
});

Trouble updating Mongodb properly

plnkr
I am trying to traverse through a collection, and update each document respectively.
My UserProfile collection consists of multiple JSON objects of userProfiles. As you can see, each profile has a lot of the same information. The only difference is the personal information. (This is just a test case of hard coded objects. The real data will be in an SQL DB managed by a sysadmin).
What I am trying to do is write a function (replaceTopics) that will take in an array of topics and replace each topic that matches in the collection. So if the system admin makes a change to a topic/s, he will send me the topic/s and I will be checking each document in my userProfile collection to see if that document has the matching topic (by matching topicIDs), if so, I need to replace that entire topic with the editedTopic.
I have tried this but with no luck. You can take a look at my function.

EnsureIndex for likes in MongoDB

well, i am creating a network that allows users creating posts and like them.
Asking on stackoverflow i've understood how to structure my database:
A collection which includes a document for each post.
A collection which includes a document for each like, in each of these documents there is a reference to post is referenced to.
When i want to get ALL likes about a post i can query the like collection looking for the reference to that post.
And till here i am ok. But assuming i'll have millions documents in like collection, i wondered how could i query and search among them in not too long time.
And i was advised of ensureIndex, in this case, i have to ensureindex of the field which contains reference to a post.
But when do i have to create this index? is enough to create it once (for example when i set up my database) and it will be as default in mongodb or do i have to do it during application life-time? thank you
But assuming i'll have millions documents in like collection, i wondered how could i query and search among them in not too long time.
I assume you would most likely want to do a count on the likes as an example?
You can't, instead you use optimizations to combat this. A count on millions of rows might get a bit slow.
A typical scenario are counters in SQL techs that you use to amend the parent row with a sum figure of its children.
Same applies to MongoDB.
You would aggregate important data to the top.
If you require to actually query the likes to show some who have liked it then you limit those likes. Google+ and other networks tend to limit the amount of likes they show to about 1,000.
And i was advised of ensureIndex,
Adding indexes to a database does help with actually searching for documents.
But when do i have to create this index? is enough to create it once
Yes, MongoDB will manage the index itself. You only need to ensure it once.

Categories