Identity tracking in NOSQL db (firebase)

One thing I'm struggling with Firebase a bit (or other NOSQL dbs, I guess?) Is that identifiers are not part of the body of the "string". Therefore, if my collection looks like this:

Books |----ldJEIF |----Title: "A Tale of Two Cities" |----Author: "Charles Dickens" |----Body: "It was the best of times..." |----2difie |----Title: "Moby Dick" |----Author: "Herman Melville" |----Body: "Call me Ishmael..." 

If I extract the BooksList and then select myBook = books[ldJEIF] to do something with the data, myBook has no idea in which list it appeared. If later I want to add it to the UserLibrary , for example, I must either de-normalize my data, or do some kind of reverse search, or pass ldJEIF around the book object and constantly search for data, Am I missing something? What is the best way to solve this problem?

+7
source share
1 answer

Grab the id when you get the data

For Firebase, at least when you retrieve the entries, you also get the id. Therefore, a simple solution is to save the identifier. If you don’t have a convenient place for this, you can simply insert it into the data:

 firebaseRef.on('child_added', function(snapshot) { var data = snapshot.val(); data.id = snapshot.name(); // add the key as an id }); 

Of course, if you do this, you must remember that you need to return it before sending the data back to the server.

Use snapshot

Firebase-specific again, you can save the link to the snapshot and pass this in place of the data. I do not really like this approach personally, but I could not cover up what neat internal principle it violates.

In some cases, this is very convenient, since at any time you will have a link to the data, id and the Firebase object; quite comfortable.

Put the identifier in the data

A common practice in NoSQL is simply to insert identifiers into the data - there is nothing wrong with that, except for a small additional storage space - in most cases the use is negligible. Then, when you retrieve the records, the identifier is already in the data, and everything is in order.

For Firebase, you can generate an identifier and put it in the data at creation time. The following trick came from one of their open sources:

 var data = {...}; var id = firebaseRef.push().name(); // generate a unique id based on timestamp data.id = id; // put id into the data firebaseRef.child(id).set(data); 
+16
source

All Articles