- Advanced Node.js Development
- Andrew Mead
- 542字
- 2021-08-27 19:05:55
Qureying todos by id
The first thing we need to do is remove everything from our query object; we no longer want to query by the completed value. Instead, we're going to query by the _id property.
Now, in order to illustrate this, I'm going to grab the ID of the Todo with the completed value of false from the Terminal. I'm going to copy it using command + C. If you're on Windows or Linux, you might need to right-click after highlighting the ID, and click Copy text. Now that I have the text inside of the clipboard, I can head over to the query itself. Now, if we try to add the ID like this:
db.collection('Todos').find({_id: ''}).toArray().then((docs) => {
It is not going to work as expected because what we have inside of the ID property is not a string. It's an ObjectId, which means that we need to use the ObjectID constructor function that we imported previously in order to create an ObjectId for the query.
To illustrate how that's going to happen, I'm going to go ahead and indent our object. This is going to make it a little easier to read and edit.
db.collection('Todos').find({
_id: '5a867e78c3a2d60bef433b06'
}).toArray().then((docs) => {
Now, I'm going to remove the string and call new ObjectID. The new ObjectID constructor does take an argument: the ID, in this case, we have it stored as a string. This is going to work as expected.
db.collection('Todos').find({
_id: new ObjectID('5a867e78c3a2d60bef433b06');
})
What we're doing here is we're querying the Todos collection, looking for any records that have an _id property equal to the ID we have. Now, I can go ahead and save this file, give things a refresh by running the script again, and we'll get the exact same Todo:
I can go ahead and change it for the Walk the dog Todo by copying the string value, pasting that inside of the ObjectID constructor function, and rerunning the script. When I do this, I get the Walk the dog Todo returned because that was the ObjectId I queried.
Now, querying in this fashion is one of the ways we'll be using find, but there are other methods other than toArray that are available on our cursors. We can explore other ones by heading over to the docs for the native driver. Inside of Chrome, have the MongoDB docs pulled up—these are the docs I showed you how to access in the previous chapter—and on the left-hand side, we have the Cursor section.
If you click that, we can view a list of all the methods available to us on that cursor:
This is what comes back from find. At the very bottom of the list, we have our toArray method. The one that we're going to look at right now is called count. From previous, you can go ahead and click count; it's going to bring you to the documentation; the documentation for the native driver is actually really good. There is a complete list of all the arguments that you can provide. Some of them are optional, some of them are required, and there is usually a real-world example. Next, we can figure out exactly how to use count.