Fetching data from MongoDB 3.6

Abhishek Prasad
3 min readApr 18, 2018

--

I recently started making making a full-stack React-Native app
(https://github.com/abhishek71994/team-despicable-us) using Mongo as a backend and Express for server-side. Having had some experience with Mongo I thought that establishing connection and fetching data would be easy… But I couldn’t have been more wrong about that.

Things have changed in mongo and fetching data from the database is not how it used to be. I got things to work after 2 hours of looking at my screen(mostly wondering what was wrong), pacing briskly in my room and complaining to my girlfriend about the developer life and why I shouldn’t have made that choice, how I would have been so happier and could have had a life.

After getting the data from the database, post which I gifted myself an extra thousand calories in the form of a pizza, I thought I would write something about the fetching of data in MongoDB 3.6 as I couldn’t find anything very useful for a beginner myself. So here is my humble effort to start things off.

We are using MongoClient to fetch data from the backend.

Lets see how things used to be:

MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("customers").find({ field:'Value' }, function(err, result) {
if (err) throw err;
console.log(result.name);
db.close();
});
});

So when you connected to the DB url the database was returned to you and you could get the collection from that and just select a collection, then run the find function to match it to a particular field of value.

Well, not anymore!!!

Let me introduce you to something called Cursors.

What that is, according to the documentation,is a pointer to the result set of a query. Clients can iterate through a cursor to retrieve results. By default, cursors timeout after 10 minutes of inactivity.

It has a bunch of new security features and the fact that it becomes inactive after 10 mins should save some bandwidth space.

But how to get them to work. Using the above example as the pivot. Let’s see how that can be done.

//old wayMongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("customers").find({ field:'Value' }, function(err, result) {
if (err) throw err;
console.log(result.name);
db.close();
});
});
//new wayMongoClient.connect(url, (err,client) =>{
if (err) console.log(err);

const db = client.db('*The DB that needs to be connected*');

db.collection('customers').find({ field:'Value' }).toArray((err, data) =>{
if (err) console.log(err)
else{
data.forEach(
(doc) => {
console.log(doc.name);
}
);

}
});

So… Let me try to explain what’s happening.
The call to the DB server url returns a client and that clients has the list of DBs in the server. We select that DB using the client.db() method and store it in const db.
Now we refer to the collection in that DB, here ‘customers’ and give the field value to be searched for.
Then it returns a promise which is then converted to an array using toArray() and it has the cursor which contains the result of that query, which is stored in data.
We have to iterate the array in order to see the results and i’m using forEach().

Hence, this is how we get the data from the DB in Mongo 3.6.

I hope this helps others and that’s the reason behind writing this blog. I have to say that the inspiration to write this, came from Kent C. Dodds . That man made a testing library for React just because he thought that it would simplify the process. So I thought, why not write about something I had problems with myself and couldn’t find a solution that easily.

Here is a link to his library — https://github.com/kentcdodds/react-testing-library
Be sure to check it out! I am planning on writing a blog after trying it out!

--

--

Abhishek Prasad

Full-stack software engineer @carsomeMY , newbie lifter, still trying to figure things out and sow things to reap