Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Recently, I had been working with a database I had created several months before. For that particular project, I had created a separate database collection for each group of items. At the time, I had a collection of books and articles, sorted by topic, and for some reason, had put all books and articles in separate collections based on topic. Then, I created endpoints for each topic. That way, I can switch out the url collection for the topic the user searched for, like this:
In this case, the user searched for âhow to put on pantsâ. But having a collection with the topic of âhow to put on pantsâ, I now can fetch just those 4 results, instead of fetching the entire library. Another component I used to make this work was a word map object. I wonât get into that here, but if youâre interested, hereâs where you can check that out: https://hackernoon.com/creating-more-accurate-search-results-for-small-sites-436e64da79b6
Although this works great, I recently wanted to be able to search all of the collections at once, which wasnât easy with so many separate collections. Basically I wanted to combine all the collections into one single collection.
I started off by doing a fetch which listed all of the collections in the database. Iâm using mlab, and the way to do that if youâre using mlab is as follows:
To get the collections in the specified database:
GET /databases/{database}/collectionsExample:https://api.mlab.com/api/1/databases/my-db/collections?apiKey=myAPIKey
So your code in implementation would look something like this:
fetch('https://api.mlab.com/api/1/databases/my-db/collections?apiKey=myAPIKey').then(function(response) {if (response.status != 200) {window.alert("Oopsie daisy");return ;}response.json().then(function(data) {let api = data;
If you console.log your api at this point, youâll have an array of each and every one of your collection names as strings. Perfect. Now Iâm going to do a for loop, and loop through those collection names, and do basically the same thing I did way up at the top, fetching in each loop iteration, and replacing the fetch collection name with the current api array item.
let arr = [];for (var i = 0; i < api.length; i++) { fetch('https://api.mlab.com/api/1/databases/my-db/collections/api[i]?apiKey=myAPIKey').then(function(response) { if (response.status != 200) { window.alert("dag nabbit"); return ; } response.json().then(function(data) { let test = data; for (var k = 0; k < test.length; k++) { arr.push(test[k]) }});});}
Here, we have our databases name in the fetch url (my-db) and in each loop, our collections name changes (api[i]). We fetch the data there and save the data as âtestâ . then, we have a nested loop, where we loop through test, and push each array item into our array called âarrâ.
Now, if we console.log âarrâ we should have a consolidated array of every single one of our collections in one place, instead of in multiple collections.
Next, weâll need to reformat all the data into a new array of JSONÂ objects.
setTimeout(function(){for (var i = 0; i < arr.length; i++) { let text = arr[i].article_text; let text2 = text.replace(/"/g, ""); let text3 = text2.replace(/'/g, ""); arr[i] = ` <p>{"articleAuthor": "${arr[i].article_author}", "articleAuthorTitle": "${arr[i].article_author_title}", "articleName": "${arr[i].article_name}", "articleDate": "${arr[i].article_date}", "articleText": "${text3}"}</p> `; document.getElementById('verseText').innerHTML += arr[i];}}, 50000);
I used a set timeout function here, to allow all of the fetches in the previous step to complete. Otherwise, I may begin building the JSON data before the fetches complete. I loop through the data contained in the array âarrâ and slap it in a paragraph with key:value pairs inside curly braces, then render each object in the DOM. Then I just copy and pasted the output into a json file. As long as there are no errors, then I should be good to go. With mlabs at this point I can just save the file and then export it to an mlabs collection using the command line prompt:
mongoexport -h ds#####.mlab.com:#### -d <database name> -c <collection name> -u <user> -p <password> -o filename.json
And weâre done. Now, I can access all of my data from a separate database, but I havenât altered the original structure with separate collections, in case I still want to use that format later.
Now, before you comment and say how dumb this method is, donât worry, I know. But, if youâre like me, and are more comfortable with the front end, this is one possible way to do this. Iâm not saying itâs the best, but itâs one way, and it works.
Combining MongoDB Collections was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.