Read Flashcards
Read first document of a collection
db.collection.findOne();
.find() && .findOne() are identical, except .findOne() returns the first document only.
Read all documents from a collection.
What is a cursor?
db.collection.find();
.find() returns a “cursor” to the entire collection of documents. A cursor is an iterable object, so you don’t pull every document out at once.
In Mongo shell, if the cursor is not assigned to a variable, the cursor will return the first 20 documents (unless specified otherwise)
What is a batch?
How do you get your next batch?
Get a cursor, get a “batch” from a cursor, check if you can get another document from the cursor, check how many documents there are left in your current batch, and get a document from the cursor.
Iterate documents in batch pattern.
A batch is the results of your cursor, as defined by your limit(), batchSize(), or the default batch size–1 megabyte on first batch (about 101 docs) and 4 megabytes on subsequent batches.
.next() is used to get the next document from a batch, but if there are no more documents in the current batch, next() will retrieve the next batch for you, as long as there is a document to return from the cursor. hasNext() will also check both the next document in the current batch and the first document of the next batch, if there weren’t any left in the current batch.
Conversely, objsLeftInBatch() is limited to counting the documents left in the current batch, and cannot count the next batch.
// Get a cursor, with the first batch: var docs = db.coll.find(); // Get a document if you can: var doc = docs.hasNext() ? docs.next() : null; // See how many docs are left in your current batch: docs.objsLeftInBatch(); // => 100
// iterate batch pattern while ( docs.hasNext() ) { printjson( docs.next() ); }
Read documents with “criteria”
db.collection.find({field : value});
Value should be the same datatype of the value in the documents when performing simple equality. Value could be more complex when not using criteria of equality.
.find() will return an array of objects.
Read documents with “projection”. What is projection?
“Projection” is the fields you would like returned.
db.collection.find(null, {field : 1});
Projection field values can be 0 or 1; 1 is to whitelist, 0 is to blacklist. Except when blacklisting “_id” only, you cannot mix whitelist and blacklist projections.
Return only specified number of documents, say 5.
db.collection.find().limit(5);
Return documents in ascending order
db.collection.find().sort({field : 1});
Return documents in descending order
db.collection.find().sort(field : -1});
What is a “data aggregation feature”? What are some examples Mongo provides?
Beyond basic queries, they provide additional functionality like counting documents, returning distinct values, or process through a pipeline.
What is a “query selection operator”?
Operator that specifies how the selection is made, such as $gt.
Get time object from ObjectId
ObjectId.getTimestamp();
Then use methods like .toString(), .getMonth(), etc.
Strangely, getTimestamp does NOT return a timestamp.
Count number of documents referenced by a cursor.
Does this run a new query?
What does the optional parameter do?
.count()
This does not perform an additional query.
This counts all docs of the cursor, NOT the batch. If you would like the count to be limited to .skip() & .limit(), use optional parameter:
.count(true)
How might “query selection operators” impact indexes?
Query selection operators that are have looser selections cannot use indexes or cannot use them well.
Examples of operators where indexes will make little to no effect include $nin and $ne, or regular expressions (with the exception of Regex that include anchors at the beginning of the string, which might use the index efficiently)
What does it mean when an index “covers” a query?
How can you tell if an index covered a query using .explain()?
An index covers a query when BOTH of the following occurs: when the query criteria is covered by the index and when all the fields returned are covered by the index.
.explain().indexOnly will be true if covered, else false.
What is a query plan, and when is it made?
How can you see all viable options of a query plan?
Before queries can be run, a query plan is made to determine which indexes should be used. The query plan is saved for future queries if there was more then one viable option (like multiple indexes could have been used).
Query plans are made when there isn’t one, when mongo restarts, indexes are added or dropped, .reIndex(), or the collection receives more than 1000 writes.
You can see all viable options of a query plan using the .explain() optional argument: .explain(true)