Get names of nodes with firebase [duplicate] - angularjs

I have the following hierarchy on firebase, some data are hidden for confidentiality:
I'm trying to get a list of videos IDs (underlines in red)
I only can get all nodes, then detect their names and store them in an array!
But this causes low performance; because the dataSnapshot from firebase is very big in my case, so I want to avoid retrieving all the nodes' content then loop over them to get IDs, I need to just retrieve the IDs only, i.e. without their nested elements.
Here's my code:
new Firebase("https://PRIVATE_NAME.firebaseio.com/videos/").once(
'value',
function(dataSnapshot){
// dataSnapshot now contains all the videos ids, lines & links
// this causes many performance issues
// Then I need to loop over all elements to extract ids !
var videoIdIndex = 0;
var videoIds = new Array();
dataSnapshot.forEach(
function(childSnapshot) {
videoIds[videoIdIndex++] = childSnapshot.name();
}
);
}
);
How may I retrieve only IDs to avoid lot of data transfer and to avoid looping over retrived data to get IDs ? is there a way to just retrive these IDs directly ?

UPDATE: There is now a shallow command in the REST API that will fetch just the keys for a path. This has not been added to the SDKs yet.
In Firebase, you can't obtain a list of node names without retrieving the data underneath. Not yet anyways. The performance problems can be addressed with normalization.
Essentially, your goal is to split data into consumable chunks. Store your list of video keys, possible with a couple meta fields like title, etc, in one path, and store the bulk content somewhere else. For example:
/video_meta/id/link, title, ...
/video_lines/id/...
To learn more about denormalizing, check out this article: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html

It is a bit old, and you probably already know, but in case someone else comes along. You can do this using REST api call, you only need to set the parameter shallow=true
here is the documentation

Related

Structuring the Firestore: Should I make another collection to store the changes that were made?

I am using Reactjs and Firestore.
I have this collection of products:
The colorMap is a map then below it are the different colors and their quanty.
Now, I want to create a list or a history whenever a product is added and whenever the quantity in those colors was added more of it.
Should I add another collection that will store when a product is added or whenever quantities are added in the color? I'm also thinking of adding a createdDate
Or there any other way I could do this? As much as possible, I won't be using any cloud functions.
A common way to keep the history of each document is by creating a subcollection under that document (say history) and writing a new document with either the complete, old document data there for every update you perform, or a new document with just the old values of the fields that were modified.
While it is convenient to do this from Cloud Functions, as they already get both the previous and the new data for each document write, you can accomplish the same from client-side code too.

How should I build a Fetch (Post) data array/structure from scratch? How do I join two arrays? Is it necessary for Fetch?

I need to build some kind of data structure/array starting with nothing and from a loop where the data is available. These would be key/value pairs. I tried initializing an empty cartData array [] in my javascript file, then using push, but this does not appear to be the right thing. This is a shopping cart program. I have all of the customer data captured in an array that looks like {First Name: firstName, Last Name: lastName, Email: emailAddress,... ,} and from the console it appears to have worked and in that form. For cartData, I'm getting something like
{Book_Title1: Price1},{Book_Title2: Price2},...{},{}
as a collection of objects. When I added to the cartData = [] array I gathered the data inside the loop and used the following method
cartData.push({[purchaseItem]: purchasePrice})
to add new items to the empty array. What I get is a collection of objects that look like
{BookTitle1: Price1}, {BookTitle2: Price2},...{}, {}
are the {}brackets unnecessary? There seem to be a number of opinions without examples. I was assuming that the first structure shown here is what is needed for Fetch to post the way a normally presents data. I could be wrong on that also. I need to know the correct way to do it and a way to combine the two data sets (three if we count the Key/Value pair for grand $total.) My goal is to have the customer hit the purchase button, the cart/form data to go to Formspree.io and for them to send an email with all of this order information. It works well enough with an unadulterated . I just need to amend the data for the cart.
It would help if you provide a snippet of the code that you've written, because it's not really clear what format are you sending the data in.
Your question seems to imply that you're sending the request body as an array. That is incorrect. It has to be a JSON.
Secondly make sure you add the 'Content-Type: application/json' header to your request

How to delete certain children of Firestore field array using Flutter

I am trying to make a Flutter and Firebase fitness application that saves runs a user logs. The runs are saved in an array called 'runs' in the Firestore database. Each document in my database represents a different user's runs, so the 'runs' array is a field found within each user's document. I have been trying to delete only a specific child of 'runs' but have had no luck. Does anyone know how to do this?
Here is my code for the delete function:
final CollectionReference runCollection = Firestore.instance.collection('runs')
Future deleteRun(dynamic runToDelete) async {
return await runCollection.document(uid).setData({
'runs': FieldValue.arrayRemove([runToDelete])
});
}
When I run this, I get no errors but nothing happens in the database.
I have also tried FieldValue.delete() but have not been able to isolate a specific index of 'runs' to delete without deleting the entire array.
Here is a picture of my firestore database:
Firestore.instance.collection(collection name).document(your document id).updateData({"array name": FieldValue.arrayRemove([delete object/array])}).than((value){print("delete");}).catchError((error){print(error);});
FieldValue.delete() only works when you know the entire contents of the array item to delete. It does not work with indexes, nor does it work with child values of array items.
What you will have to do instead is read the document, modify the array in memory, then write the modified array back to the document.

aws DynamoDb Boto adding a new item to a table

I am working to add items to a dynamodb database using python (boto api). I saw examples of people creating items and storing them using the table.new_item method.
Ex:
dynamoConn = boto.connect_dynamodb(aws_access_key_id, aws_secret_access_key)
dTable = dynamoConn.get_table(aws_dynamo_table)
....
item_data = {}
....
dTable.new_item(loc, theNewKey, item_data)
The code runs, I do not find any errors and when tracing through using debugger I do not see any reason why my item, a hash of keys and text values can't be stored.
I read: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithTables.html
However I think I may be missing an "update table" function. I'm not seeing one nor can I find an example of one online.
Any ideas?
The new_item method returns an Item object that is ready to be saved to DynamoDB but it doesn't actually save it for you. Once you are ready to save the item, you need to call the save (UpdateItem) or put (PutItem) method of the Item object.

Find CouchDB docs missing an arbitrary field

I need a CouchDB view where I can get back all the documents that don't have an arbitrary field. This is easy to do if you know in advance what fields a document might not have. For example, this lets you send view/my_view/?key="foo" to easily retrieve docs without the "foo" field:
function (doc) {
var fields = [ "foo", "bar", "etc" ];
for (var idx in fields) {
if (!doc.hasOwnProperty(fields[idx])) {
emit(fields[idx], 1);
}
}
}
However, you're limited to asking about the three fields set in the view; something like view/my_view/?key="baz" won't get you anything, even if you have many docs missing that field. I need a view where it will--where I don't need to specify possible missing fields in advance. Any thoughts?
This technique is called the Thai massage. Use it to efficiently find documents not in a view if (and only if) the view is keyed on the document id.
function(doc) {
// _view/fields map, showing all fields of all docs
// In principle you could emit e.g. "foo.bar.baz"
// for nested objects. Obviously I do not.
for (var field in doc)
emit(field, doc._id);
}
function(keys, vals, is_rerun) {
// _view/fields reduce; could also be the string "_count"
return re ? sum(vals) : vals.length;
}
To find documents not having that field,
GET /db/_all_docs and remember all the ids
GET /db/_design/ex/_view/fields?reduce=false&key="some_field"
Compare the ids from _all_docs vs the ids from the query.
The ids in _all_docs but not in the view are those missing that field.
It sounds bad to keep the ids in memory, but you don't have to! You can use a merge sort strategy, iterating through both queries simultaneously. You start with the first id of the has list (from the view) and the first id of the full list (from _all_docs).
If full < has, it is missing the field, redo with the next full element
If full = has, it has the field, redo with the next full element
If full > has, redo with the next has element
Depending on your language, that might be difficult. But it is pretty easy in Javascript, for example, or other event-driven programming frameworks.
Without knowing the possible fields in advance, the answer is easy. You must create a new view to find the missing fields. The view will scan every document, one-by-one.
To avoid disturbing your existing views and design documents, you can use a brand new design document. That way, searching for the missing fields will not impact existing views you may be already using.

Resources