Read Flashcards

0
Q

Read first document of a collection

A

db.collection.findOne();

.find() && .findOne() are identical, except .findOne() returns the first document only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

Read all documents from a collection.

What is a cursor?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

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() );
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Read documents with “criteria”

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Read documents with “projection”. What is projection?

A

“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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Return only specified number of documents, say 5.

A

db.collection.find().limit(5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Return documents in ascending order

A

db.collection.find().sort({field : 1});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Return documents in descending order

A

db.collection.find().sort(field : -1});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a “data aggregation feature”? What are some examples Mongo provides?

A

Beyond basic queries, they provide additional functionality like counting documents, returning distinct values, or process through a pipeline.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a “query selection operator”?

A

Operator that specifies how the selection is made, such as $gt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Get time object from ObjectId

A

ObjectId.getTimestamp();

Then use methods like .toString(), .getMonth(), etc.

Strangely, getTimestamp does NOT return a timestamp.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Count number of documents referenced by a cursor.

Does this run a new query?

What does the optional parameter do?

A

.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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How might “query selection operators” impact indexes?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does it mean when an index “covers” a query?

How can you tell if an index covered a query using .explain()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a query plan, and when is it made?

How can you see all viable options of a query plan?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the 2 general ways to read from the DB?

A

.find() - a single query on a single collection
& the aggregation framework, which allows you to process multiple queries on at least 1 collection through a pipeline.

There’s also technically mapReduce but aggregations can do everything mapReduce can and its faster. (Remains for legacy purposes I believe)