[00:00] In order to insert documents into the collection, we're going to walk into the collection object itself and use either the insert one or insert many methods. Now both of them accept the first parameter which is going to be either the document or the document array itself. So I have created some predefined objects. And here we have a const new employee. I'm just going to copy whatever is the content of the JSON itself.
[00:30] This is just a regular JSON with some ID, name, position, etc. And a personal info nested objects. So let's grab this one and let's paste it over here. So after running this command, we can see that Mongo has acknowledged this, which means basically it has been confirmed that the write operation was successful and there is an inserted ID saying that there is an object ID with some hash value. Now if we want to see what are the documents within the collection, we'll run DB employees and one of the possibilities is to count the documents which we'll see there is already one document or we can run the find method Which will return the object itself The first interesting thing here is that it has kind of ignored our ID, it just treated it as if it was a relevant data, but Mongo is going to insert the underscore ID, the object ID for every document anyway.
[01:33] So let's try to add some two other objects. So they are going to include slightly different data but they have the very same shape, the same properties. So let's now use the DB employees and let's insert many documents so now we pass an array of documents so when we run it we will see that acknowledged true again inserted IDs and we can see that these IDs have been added into this objects again. So let's take a look. Db.employees.
[02:08] And now let's run find and we will see that, hey, yes, for the ID 13, for ID 12, this underscore ID properties, the primary keys have also been added. So let's now clear the collection and we would run db.employees and delete either one or many. Both of these methods accept a filtering criteria, the query itself to identify which documents should be deleted and if we run deleteMany without providing what is the query to identify the objects, then it would throw the error. However, we can, as for now, just to make it simpler, provide a query that would make all documents to be matched. Now, this means that all existing documents have been deleted.
[03:00] So acknowledge true, delete at count 3. So let's now add the same objects. I'm just going to strip the unnecessary ID property and I'm also going to copy not only the JSON itself but also this const variable just to show that mongochel is capable of executing not only JSON but also JavaScript code so every piece of JavaScript syntax could be already supported by Mongo and obviously the const and the methods on arrays, objects, etc. They're all available. So here I'm going to insert many using this variable.
[03:43] So that's going to be db.employees and now insert many and I'm just going to pass the new employees so here we have the inserted ID so there are two documents being inserted if we run DB employees find then we will see that they are correctly inserted without our stripped ID property. Now, if we walk into the employees tab, we might need to click the find button over here and we can see our documents existing in the database, or we can go into the local and now we might need to basically refresh over here to reflect that there are new documents being created. In any case we can also create new documents using the GUI, for instance, insert document, and here we go with the form to define what is the content of the document.