Week 4 L1 Flashcards
How to insert a document ?
.insert
How to insert datasets such as JSON CSV and TSV?
use mongo import
how to specify format of import?
/type
What happens if no collection name is given ?
the collection is named from file without extension
How do you remove any blanks from insert?
/ignoreBlanks
what does upserting data mean ?
/mode upsert
Upsert inserts new data but replaces any matching data. Inserts if no match or updates if it does.
How to specify fields for upsert?
/upsertFields
How to specify field names?
/fields name
How to specify list of field names
/headerline
How to export data?
mongoexport
How to export part of a collection
/query
How to export a subset of names of fields
/fields title
how to sort a export ?
/sort
how to reduce number of exported documentes
/skip and /limit
What are indexes
Special data structures that store small portions of collections data set in a easy to traverse form. Indexes store values of specific fields or set of fields ordered by value of field. Each entry in index points to DB document.
Why do we use indexes?
To support efficient execution of queries in mongo db.
Without indexes Mongodb must scan every document in a collection to select those documents that match the query criteria.
At what level does mongodb define its indexes?
At collection level
What structure do indexes in mongo db use?
B-Trees
What is the big O notation of a non index look up ?
O(N)
What is the big O notation when using a B tree ?
O(log n)
Benefits of index
Held In ram
Results are also already sorted so no need for additional sort after query
Covered results do not need to access DB at all, just return data from the index.
Range searches are quicker
How do we measure the efficiency of a search ?
Using the explain method
What are the different types of index in mongodb?
Default ID index Single field index , compound index, multikey index- index array fields, geospatial index, text indexes, hashed indexes
What are some examples of mongodb index properties?
unique indexes- reject duplicate values from index fields.
sparse indexes- only index docs that have a index fields, so only contains subset of whole collection
why should you be careful with the amount of indexes you use?
should only index what oyu need as too many indexes can fill ram.
Index should be for frequent and targeted queries and updates.
Indexes will slow down large number of insert updates and deletes as the index will also need to be changed.
What is index hinting ?
If you search across many fields and there are more than one field with index and there’s no compound index then hint tells mongo db optimises which index to use