- Advanced Node.js Development
- Andrew Mead
- 186字
- 2021-08-27 19:05:53
Calling the .getTimestamp function
What we're going to call is .getTimestamp. The getTimestamp is a function, but it doesn't take any arguments. It simply returns the timestamp that the ObjectId was created at:
console.log(result.ops[0]._id.getTimestamp());
Now, if we go ahead and rerun our program, we get a timestamp:
In the preceding screenshot, I can see that the ObjectId was created on February 16th 2016 at 08:41 Z, so this timestamp is indeed correct. This is a fantastic way to figure out exactly when a document was created.
Now, we don't have to rely on MongoDB to create our ObjectIds. Inside of the MongoDB library, they actually give us a function we can run to make an ObjectId whenever we like. For the moment, let's go ahead and comment out our call to insert one.
At the very top of the file, we're going to change our import statement to load in something new off of MongoDB, and we're going to do this using an ES6 feature known as object destructuring. Let's take a quick second to talk about that before we actually go ahead and use it.