Firestore - saving external links in a document with both url and title - google-cloud-firestore

How can I create an array in firestore that can save external links?
I want to save the title and the href so that I can use both values in the read.
I want the links attribute to be an array so that I can save multiple links. Is it possible to do that in a single firebase attribute, or should I create separate link attributes with a href and title for each one (ie firstLinkTitle, firstLinkHref)?

It's not possible to have a number of links, that would be a different array - similar to an array of arrays.
The possibility for the creation of a field of type array for your documents, you can either use the console, where this type is available or via code as the following example:
const data = {
stringExample: 'Example',
arrayExample: ['Title', 'href']
};
const res = await db.collection('data').doc('one').set(data);
With the above example, you should be able to create a field of type array and set the data to it.
Besides that, if you want to update data from your array without uploading all the data again, you can use the function arrayUnion() or use the arrayRemove(), to remove elements from it.
You can get more information on how to work with arrays in Firestore, by checking the below documentation.
Add Data
In case you need something like nested arrays, indeed, you will need to have a "child" collection that will be linked to the father one.
Let me know if the information helped you!

Related

Validating arrays using Firestore rules

In my HTML page I have a group of five checkboxes. Their values are 1,2,3,4,5.
In the object to which those checkboxes belong I save them as an array. So if user selects options 2,4,5 I store it as [2,4,5].
I'm stuck at the spot when I try to validate that array in Firebase rules. Is there any way of doing validation like this?
I want to make sure an array like [5,6,7] does not go through the Firestore rules but something like [1,3,4,5] does.
Array type fields in Firestore documents show up as List type objects in security rules. You can use its hasOnly method to determine if the List contains only certain elements.
request.resource.data.arrayField.hasOnly([1,2,3,4,5)

MongoDB Compass Visually Insert Sub-Document

I'm using MongoDB Compass to visually build my collection.
In the highlighted section, I don't see a "Document" type to insert a sub-Document. Am I doing it the right way?
The docs state :
For the field values, you can paste valid JSON that can be parsed by
JavaScript’s JSON.parse.
If the pasted JSON is an Object (i.e. a document), the keys must be
quoted in double quotes and are permitted to be escaped. The first
character must be left curly brace { and the last must be right curly
brace }:
{
"email": "test#example.com",
"phone": "555-555-1212"
}
But unfortunately the process is really counterintuitive, because if you copy the JSON object from the above example, you will not be able to paste it inside the field value unless you firstly type something i.e. a space character.
But if your object is not a valid JSON, it will be pasted (!) but not parsed (?) and it will be saved as a string.
Anyway even if you manage to store your object, you will not be able to edit it easily, and you will end up copying and pasting to an external text editor in order to achive your goal.
EDIT :
You can try using this client Robo 3T which lets you edit the document structure much more easily. Funny thing, if you edit the document with Robo 3T and reload the document in Compass, it will let you do what you intended all along... So it must be a kind of Compass UI issue/bug.
You can achieve what you mean, just setting otherQuestions type as Object.
This will let you to embed another document/object , It is a JSON
This is the way to work with a NoSQL BSON based database, it is not relational, so you can't set a field type as another document, just use Object and embed there the tree as you want.
Anyway I highly recommend reading Mongo's documentation
You can select the Object type while editing from the UI as below:
The Object can have multiple attributes of different types. When queried using an API, the data comes as below:
You can simply assign a type object to the field/variable name for that sub-document
MongoDB Compass seems to have recently added the JSON view feature:
So, switch from the List view to that, then hit Edit Document on whichever document you want to change. You are now editing the document's JSON representation directly. Hit Update when you're done with your change, assuming the JSON is still valid after your changes.
This helps a lot if you need to add larger pieces of data to existing documents for example (arrays of objects, etc.) versus adding one prop at a time and selecting types from minuscule dropdowns.
Better late than never: I think the fastest way to insert complex data into a MongoDB collection is simply to export the table, then truncate the table (delete all records), modify whatever you want in a nice text editor (notepad++, sublime, etc) and finally import the data back into the table.
Done! :)

How to write nested Graph API request

I am trying to create nested FB Graph API request to get images for public event.
The full JSON object is given at this gist: https://gist.github.com/ZeKoU/be92b88440a6ca3d6be3
What I am trying to do is to get only data.0.images object, i.e. I want to get first object from data , then to get images array, and then to pick some fields from there (source for example).
However, all my attempts (see picture below) are returning only two fields for each object inside data field.
Not sure what you're trying to do, but from what I understood the query
/310897782446456?fields=photos.fields(id,images{source}).limit(1)
should be an example of getting the first photo, extract only some fields from the object (id and images for example), and then get a single field (source for example) from the images array's objects.
https://developers.facebook.com/tools/explorer?method=GET&path=310897782446456%3Ffields%3Dphotos.fields(id%2Cimages%7Bsource%7D).limit(1)&version=v2.3
See
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#fieldexpansion

Firebase: how to generate a unique numeric ID for key?

I need numeric IDs for human readability. How do I get it in Firebase?
I want numeric ID for keys, e.g. "000000001", "000000002","00000003","00000004".
The reason I need it is because these IDs will become the permanent object ID both online and offline. I want users to be able to browse that object page by just entering URL "/objects/00000001" without efforts.
I am asking here, because I want to know if this can be done without using .priority, sub-properties, etc. I guess set method can do it somehow. If it is not possible, just tell me no, I can accept that answer.
I'd suggest reading through the Firebase documentation. Specifically, see the Saving Data portion of the Firebase JavaScript Web Guide.
From the guide:
Getting the Unique ID Generated by push()
Calling push() will return a reference to the new data path, which you can use to get the value of its ID or set data to it. The following code will result in the same data as the above example, but now we'll have access to the unique push ID that was generated
// Generate a reference to a new location and add some data using push()
var newPostRef = postsRef.push({
author: "gracehop",
title: "Announcing COBOL, a New Programming Language"
});
// Get the unique ID generated by push() by accessing its key
var postID = newPostRef.key;
Source: https://firebase.google.com/docs/database/admin/save-data#section-ways-to-save
A push generates a new data path, with a server timestamp as its key. These keys look like -JiGh_31GA20JabpZBfa, so not numeric.
If you wanted to make a numeric only ID, you would make that a parameter of the object to avoid overwriting the generated key.
The keys (the paths of the new data) are guaranteed to be unique, so there's no point in overwriting them with a numeric key.
You can instead set the numeric ID as a child of the object.
You can then query objects by that ID child using Firebase Queries.
From the guide:
In JavaScript, the pattern of calling push() and then immediately calling set() is so common that we let you combine them by just passing the data to be set directly to push() as follows. Both of the following write operations will result in the same data being saved to Firebase:
// These two methods are equivalent:
postsRef.push().set({
author: "gracehop",
title: "Announcing COBOL, a New Programming Language"
});
postsRef.push({
author: "gracehop",
title: "Announcing COBOL, a New Programming Language"
});
Source: https://firebase.google.com/docs/database/admin/save-data#getting-the-unique-key-generated-by-push
As explained above, you can use the Firebase default push id.
If you want something numeric you can do something based on the timestamp to avoid collisions
f.e. something based on date,hour,second,ms, and some random int at the end
01612061353136799031
Which translates to:
016-12-06 13:53:13:679 9031
It all depends on the precision you need (social security numbers do the same with some random characters at the end of the date). Like how many transactions will be expected during the day, hour or second. You may want to lower precision to favor ease of typing.
You can also do a transaction that increments the number id, and on success you will have a unique consecutive number for that user. These can be done on the client or server side.
(https://firebase.google.com/docs/database/android/read-and-write)
Adding to the #htafoya answer.
The code snippet will be
const getTimeEpoch = () => {
return new Date().getTime().toString();
}
As the docs say, this can be achieved just by using set instead if push.
As the docs say, it is not recommended (due to possible overwrite by other user at the "same" time).
But in some cases it's helpful to have control over the feed's content including keys.
As an example of webapp in js, 193 being your id generated elsewhere, simply:
firebase.initializeApp(firebaseConfig);
var data={
"name":"Prague"
};
firebase.database().ref().child('areas').child("193").set(data);
This will overwrite any area labeled 193 or create one if it's not existing yet.
https://firebase.google.com/docs/firestore/manage-data/transactions
Use transactions and keep a number in the database somewhere that you can increase by one. This way you can get a nice numeric and simple id.

Is it possible to store hidden metadata information that is tied to a specific Table or Cell within a Word document?

I am trying to store metadata (basically a unique id) along with each cell of a table in a Word document. Currently, for the add-in I'm developing, I am querying the database, and building a table inside the Word document using the data that is retrieved.
I want to be able to save any of the user's edits to the document, and persist it back to the database. My initial thought was to store a unique id along with each cell in the table so that I would be able to tell which records to update. I would also like to store some sort of "isChanged" flag within each cell so that I could tell which cells were changed. I found that I could add the needed information into the "ID" property of the cell - however, that information was not retained if the user saved the document, closed it, and re-opened it. I then tried storing the data by adding a data to the "Fields" collection - but that did not work and threw a runtime error. Here is the code that I tried:
object t1 = Word.WdFieldType.wdFieldEmpty;
object val = "myValue: " + counter;
object preserveFormatting = true;
tbl.Cell(i, j).Range.Fields.Add(tbl.Cell(i, j).Range, ref t1, ref val, ref preserveFormatting);
This compiles fine, but throws this runtime error "This command is not available".
So, is this possible at all? Or am I headed in the wrong direction?
Thanks in advance.
Wound up using "ContentControls" to store the information I needed. I used the "Title" field to store the unique id, and the "tag" field to track whether the field had been changed or not. See this link for more info: http://blogs.technet.com/gray_knowlton/archive/2010/01/15/associating-data-with-content-controls.aspx
Since a "Word 2007 Document" is XML, you can add a namespace to the document then adore the elements with attributes from your namespace. Word should ignore your namespace when loading and saving. Moreover, you can add new elments to store any information (metadata) needed.
With that said, I have not used this technique with Word, but I have done it successfully using Excel 2003.
First thing to try, is create a bare "Word 2007 Document". In your case, add a simple two by two table. Open it with a text or XML editor and add your namespace, and adore an attribute and add an element. Open with Word make a change then save it. Open with editor and make sure your namespace attribute and element have not been changed.