- Advanced Node.js Development
- Andrew Mead
- 702字
- 2021-08-27 19:05:52
Adding a new record into a collection
Inside of Atom, what I'd like you to do is take the code all the way from db.collection down to the bottom of our callback, and comment it out. Then, we're going to go ahead and add something following it. Right previous db.close(), you're going to type Insert new doc into the Users collection. This doc is going to have a few properties. I want you to give it a name property; set that equal to your name. Then, we're going to give it an age property, and last but not least we can give it a location string. I want you to insert that doc using insertOne. You're going to need to pass in the new collection name into the collection method. Then, further down, you're going to add some error-handling code, and you're going to print the ops to the screen. Once you rerun the file, you should be able to view your record in the Terminal and you should be able to refresh things. Over in Robomongo, you should see the new Users collection, and you should see your user with the name, age, and location you specified.
Hopefully, you were able to insert a new document into the Users collection. What you needed to do in order to get this done is call db.collection so we can access the collection we want to insert into, which in this case is Users:
//Insert new doc into Users(name, age, location) db.collection('Users')
Next up, we have to call a method to manipulate the Users collection. We want to insert a new document, so we're going to use insertOne, just like we did in the previous sub-section. We're going to pass our two arguments into insertOne. The first one is the document to insert. We're going to give it a name property; I'll set that equal to Andrew. Then, we can go ahead and set the age equal to something like 25. Lastly, we'll set the location equal to my current location, Philadelphia:
//Insert new doc into Users(name, age, location) db.collection('Users').insertOne({ name: 'Andrew', age: 25, location: 'Philadelphia' }
The next argument we want it to pass in is our callback function, which is going to get called with the error object as well as the results. Inside of the callback function itself, we're going to first handle the error. If there was an error, we're going to go ahead and log it to the screen. I'm going to return console.log, and then we can put the message: Unable to insert user. Then, I'll add the error argument as the second argument for console.log. Next up, we can add our success case code. If things go well, all I'm going to do is use console.log to print result.ops to the screen. This is going to show us all of the records that were inserted:
//Insert new doc into Users(name, age, location) db.collection('Users').insertOne({ name: 'Andrew', age: 25, location: 'Philadelphia' }, (err, result) => { if(err) { return console.log('Unable to insert user', err); }
console.log(result.ops); });
We can now go ahead and rerun the file inside of the Terminal using the up arrow key and the enter key:
We get our array of inserted documents, and we just have one. The name, age, and location properties all come from us, and the _id property comes from MongoDB.
Next up, I want you to verify that it was indeed inserted by viewing it in Robomongo. In general, when you add a new collection or a new database, you can just right-click the connection itself, click Refresh, and then you should be able to see everything that was added:
As shown in the preceding screenshot, we have our Users collection. I can view the documents for Users. We get our one document with the name set to Andrew, age set to 25, and location set to Philadelphia. With this in place, we are now done. We've been able to connect to our MongoDB database using Node.js, and we've also learned how to insert documents using this mongo-native library. In the next section, we're going to take an in-depth look at ObjectIds, exploring exactly what they are and why they're useful.