How can I select specific fields using the firestore modular api - node.js

In the <= V8 Firestore API I could query a set of documents and extract a subset of fields with code like the following:
const membersRef = db.collection('members');
const snapshot = await membersRef.select('First', 'Last').get();
With the new modular api I can do the following:
import { collection } from 'firebase/firestore';
const membersRef = collection(db,'members');
// how to select only First and Last from members ?
I can't find a select() function to specify fields in the new api. How can this be done using the V9 modular api?
I know I can read the entire collection and use .map()/.forEach to reduce the fields but I have some large fields I would rather not query unless I need them for a specific document. The use of select to have the server pull out only the fields needed is useful.

The client-side SDKs for Firebase don't have a select method/function, but always retrieve full documents. So the select method didn't exist in the v8 CollectionReference or Query classes, nor does it exist as a top-level function in the v9 modular SDK.
There is a select() method in the Admin SDK for Node.js, but that one still works as a method on CollectionRefence as far as I can tell.

Related

How to update this firestore document field using cloud functions?

Im new to firebase, firestore and cloud functions. How do I update the this filed called "status" based on a field called "LastUpdatedAt"
exports.hello = functions.firestore.document("RealtimeData/{anyMachine}/Params/{anyParams}")
The field "LastUpdatedAt" is inside {anyParams}: It looks like
LastUpdatedAt: 1606844285038
After conversion from timestamp
LastUpdatedAt: 1/12/2020 23:8:5
How do I write a cloud function to compare this "LastUpdatedAt" with the present time and if the time difference is greater than 15 then the 'status' should be set to offline else it should be online.
If you want to update a single field in a document then first you should retrieve the document through document ID then you can perform the update function while specifying the field.
For more information you can refer to the documentation and stackoverflow answer, where a brief description about node js SDK for Cloud Firestore has been discussed.

Dynamically accessing Mongodb database collection based in URL parameters

I have an API endpoint as api/get?subject=economics, and based on this subject parameter I access various database collections in mongodb. Right now I am using switch case statements to access the required database based on subject parameters. This is making my code very lengthy. Is there a way to access the database just by the subject parameter value? for example, instead of using this
const {subject}=req.query
switch (subject)
case "economics"
const data= await economics.find()
break;
I want to be able to use this
const {subject}=req.query
const data=await subject.find() // here subject will refrence its value like economics or stats
I think better way to pass modal as an argument. Here I is the code, How I have created common controller
https://gist.github.com/RMUSMAN/b7132fda6e945393882586c26b132e24
https://gist.github.com/RMUSMAN/c419d8149effb8514845946ad5b652f1
https://gist.github.com/RMUSMAN/f44ba2a20da35a2ce5ffd7517ea8fca8

Database modeling for firestore. Collection - categories

So i'm trying to model a database schema for firestore. It's called categories and i don't know how to store items. I tried watching https://fireship.io/lessons/advanced-firestore-nosql-data-structure-examples/. It looks logical, there are documents witch hold their children id and parent id, if parent is top level, then parent is null, but how to send data from back-end(Node.js) to front-end(Vue.js).
Is there any other approach?
You can read the data from Firestore using its service from the firebase package.
Let's say you have a collection (called a table in SQL databases) called Users and you want to access the document (called a row in SQL databases) with the id of 1. You can do this via this snippet:
import firebase from 'firebase/app'
import 'firebase/firestore'
const firestore = firebase.firestore()
const data = firestore.collection('Users').doc('1').get()
.then(snapshot => {
console.log(snapshot.data())
})
The important thing to note when working with Firestore is that there are collections and documents. Collections, for example categories_name can store multiple documents, which are stored by {id}. For example,
.collection("categories")
.doc("all")
.collection("todos")
.doc("DataForTodo")
.set(data);
For more information on exactly how to structure Firestore, please refer to the official documentation: https://firebase.google.com/docs/firestore/manage-data/structure-data

Get all items on strapi collection

I am using Strapi API on my project, and I would like export my data in CSV file format.
For this, I use a find() request, but the API returns only the first 100 items and I've ~ 2000 items in my database.
Is it possible to remove the limit on my Strapi request ?
For that, you will have to use the _limit filter and use -1 as value.
eg. strapi.query('restaurant).find({_limit: -1});
After that I suggest you export directly from your database it will be easier.

Why is data retrieved from Firestore returning empty array when there are subcollections present?

I am trying to retrieve data from my the my firestore database using angularfire2.
This is what my current database looks like. I have a users collection that contains the userId doc which binds the userDetails and userPosts together.
However when I query this collection, it returns an empty array in the console.
I am using a firebase function to retrieve the data.
Firebase Function Index.ts
export const getFeed = functions.https.onCall(async (req,res) =>{
const docs = await admin.firestore().collection('users').get()
return docs.docs.map(doc => {
return {
postID: doc.id,
...doc.data()
}
})
})
TS File
tabTwoFeedInit (){
const getFeed = this.aff.httpsCallable('getFeed')
this.ajax = getFeed({}).subscribe(data=> {
console.log(data)
this.posts = data
})
}
How can I retrieve data from this firebase database successfully?
Firestore reads are shallow, and so they won't return subcollections automatically. Thus, your get() will only return the document ID, since the document has no fields.
To return the subcollections of a document, you need to call the getCollections method on that document. This can only by done by the admin API, but that should be fine for you since you are running inside a cloud function. As the documentation notes, it is generally expected that collection names are predictable (as they appear to be in your case), but if they aren't, you might consider restructuring your data.
Why are shallow reads desirable? It makes it possible to avoid retrieving potentially large collections of information that might be associated with, say, a user, so you can structure data more naturally. Depending on the size of the data, its possible that a field that is a map might make more sense for userDetails (but a collection is probably the right thing for userPosts).
If you are just creating the cloud function to retrieve the posts from this structure. I would prefer to restructure the database a bit and just use Collection Group Query on client side (with no requirement of cloud functions) to pull the posts.
By restructuring I mean, you should store userID inside the documents of userPosts sub collection.
Then simply user Collection Group Query to retrieve post of specific users.
The syntax is of firebase javascript library. You can find it's equivalent of angularfire
let posts= db.collectionGroup('userPosts').where('userId', '==', 'some-user-id');
I also ran into the same problem but I solved it by adding a field to the particular document i am trying to retrieve.Sometimes the cause is because the documents you are trying to get has no field in it. What I mean is that a document must have a field before the server can recognize it as an existing document.
Possibly there may be no field in the document you are trying to retrieve which makes tye server say it is not existing and so will not be retrieved.
So if you run into this problem, then consider adding a field to that document before you can successfully retrieve it.

Resources