Advanced Node.js Development
上QQ阅读APP看书,第一时间看更新

Fetching todos in Robomongo file

Now, we're going to create a new file based off of mongodb-connect. In this new file, instead of inserting records, we'll fetch records from the database. I'm going to create a duplicate, calling this new file mongodb-find, because find is the method we're going to use to query that database. Next, we can go ahead and remove all of the commented-out code that currently inserts records. Let's get started by trying to fetch all of the Todos out of our Todos collection. Now, if I head over to Robomongo and open up the Todos collection, we have just one record:

In order to make this querying a little more interesting, we're going to go ahead and add a second one. Right in the Robomongo window, I can click Insert Document. Robomongo can delete, insert, update, and read all of your documents, and this makes it a fantastic tool for debugging. We can add a new document on the fly, with a text property equal to Walk the dog, and we can also tack on a completed value. I'm going to set completed equal to false:

{
text : "Walk the dog",
completed : false
}

Now by default, we're not going to provide an _id prop. This is going to let MongoDB automatically generate that ObjectId, and right here we have our two Todos:

With this in place, let's go ahead and run our first query inside of Atom.