If we have 3 fields in a document – name, age, sex We could make name or age or sex or (name, age) or (name, age, sex) as index.
Assume that we make a index out of (name, age, sex) In this case, we need to use the keys from left to right.
If we use “name” in our command, it makes use of the index. If we use (“name”, “age”) in our command, it makes use of the index. If we use (“name”, “age”, “sex”) in our command, it makes use of the index.
If we use “age” in our command, it can’t use the index for its operation. If we use (“age”, “sex”) in our command, it can’t use the index for its operation.
If we use (“name”, “sex”) in our command, it simply uses “name” field and ignores “sex” field.
For convenience, mongoDB adds “_id” field to each document inserted. “_id” is unique across the collection. And index is automatically created on “_id”.
Since index information is stored in “system.indexes” collection – it consumes disk too. So we need to make sure to add indexes to only those fields which we access frequently. Also note that, each time a document is inserted, “system.indexes” collection must be updated with the new index information, which takes time, bandwidth and disk space. So we need to be careful while creating indexes.
If we chain explain() method to our command, we can know some details about the command and also evaluate about its efficiency. The above command doesn’t have multi-key enabled and it’s a Basic Cursor.
Since “names” collection was empty, we inserted 2 documents into it. Now we applied drop() method on the collection, which drops all the document present in the collections at once. It also removes the document/index/content present inside “system.indexes” collection.
Note: If you want to remove/drop all the documents present inside the collection, make use of drop() method, as it removes all the documents at once, its more efficient than remove({}) method which removes documents one by one. Use remove() method, when you want to remove one or a set of documents from the collection.
If we leave the first argument of find() method empty, it matches with all the documents of the collection. Hence it fetched all the Company names from the documents.
Here we have 3 arguments for update() method. First one is intentionally left empty. In second argument, we specify the changes needed to the documents. In third argument we specify the option, multi: true – which tells mongo engine to match with all the documents in the collection.
Lets learn to use upsert option with update() method.
upsert basically inserts document into the collection, if there is no matching document found in the collection. If there is a match found, then the document simply gets updated – in which case upsert option will not have any effect on the selected document.
Observe the 3rd argument of update() method – upsert: true. In this case, since there is no matching document found inside the “names” collection, for “Company” Xiaomi, it inserts the document with all the fields and values specified in the update command. i.e., Company: Xiaomi, Product: Mi3, No: 3
We have documents which has values 1, 2 and 3 for the field “No”. But in above command the condition is to select documents which has “No” value above 3 – which results in 0 documents being selected. But since we have “upsert: true” as the third argument of update() method, the “field: value” – “Product: Glass” and “No: 4” gets inserted into the “names” collection. So we need to take proper care of our commands and conditions – so that unintentional insertion of data or document can be avoided.
In this case, there is a document inside the collection which has “No”: 4. So the upsert option will not have any effect on the selected data/document – it simply gets updated with the field values specified in the second argument of update() method. i.e., the document with “No”: 4 is selected and the “Company”: “Sony”, “Product”: “Smart Watch” gets added to the existing document.