MongoDB Flashcards

1
Q

For a movie collection, increase the performance of a query that returns movies released in the last 5 years with their names in sorted order.
Is it the right way?
db.movie.createIndex({released_date: -1}).sort({name: 1})

A

db.movie.createIndex({released_date: -1, name: 1});
No.
Sorting w/o indexes will be very costly as it will fetch docs from the disk and sort them in RAM. The ideal way is to use indexes not only for search but also for sorting.

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

How do you speed up specific queries in MongoDB and in Cassandra?

A

In MongoDB, create the right indexes.
In Cassandra, choose the right Primary Key = Partitioning Key + Clustering Columns.

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

For a movie collection, increase the performance of a query that returns all “romantic” movies.
Note: tags: [“violence”, “romantic”, “family”]

A

db.movie.createIndex({tag: 1})
It creates a MultiKey index on the tag key as its array field.

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

For an inventory collection, increase the performance of the query that returns all devices that has one of the WAN port down.
Note: Each device has two WAN ports i.e wan_ports : [{}, {}]

A

db.inventory.createIndex({wan_ports.status : 1})

Create an index on KEYS of embedded docs but not on docs itself.
db.inventory.createIndex({wan_ports : 1}). Bad Idea.

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

Create a user collection where each user has a unique key called “user_id”?

A

db.user.createIndex({user_id : 1}, {unique: true})
In MongoDB, uniqueness is enforced by a unique index.

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

In company collection, optimize query (while taking care of index size) to list company names with IPO.

A

db.company.createIndex({name: 1}, {partialFilterExpression: { ipo: {$exists: true}}})
Partial Index preferred, as multiple filter option available, unlike sparse indexes which provide only exists option.

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

A collection has compound index {a: 1, b: 1, c: 1} and the query is {c: 10, a: 5, b: 23}. Will the query use the index?

A

Yes.
Mongo is smart enough to read query as { a: 5, b: 23, c: 10} and hence use the index

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

A collection has compound index {a: 1, b: 1, c: 1} and the query is {c: 10, b: 23}. Will the query use the index?

A

No

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

A collection has compound index {a: 1, b: 1, c: 1} and the query is {a: 10}. Will the query use the index?

A

Yes. This is an index prefix query
Index Prefixes {a: 1}, {a: 1, b: 1}

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

In a big cluster, you need to see how a query will run without running it. How to do that?

A

db.collection.query.explain()

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

In a big cluster, you need to see how a query executes. How to do that?

A

db.collection.query.explain(“executionStats”)

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

For this query, provide various options for creating a compound index.
Query: { college = “IITD”, score > 75%, Sort on student_name }

A

{college: 1, score: 1, student_name: 1} works well in general but if the number of records to sort is too large then the sorting will happen in memory causing a spike.

Alternate Tradeoff: {college: 1, student_name: 1, score: 1}
It will read more keys from Index but there is no need for sorting in RAM as the index will provide already sorted data.
ESR: Equality, Sort, Range

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

How to create a circular ring buffer in MongoDB?

A

Using Capped Collections

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

How to log the queries on a specific database?

A

Database profiler
> use dbName
> db.setProfilingLevel(2) # logs all operations
> db.setProfilingLevel(0) # disables profiler

profiler logs into the system.profile collection
> db.system.profile.find({})

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

How do you clean up the database profiler logs?

A

db.setProfilingLevel(0);db.system.profile.drop();db.setProfilingLevel(2);

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

When reading docs with projection, does it escape reading data from the disk? Is projection server side or client side?

A

The document is read fully even with projections.
Projection is done on the server side and hence client sees only meaningful traffic.

17
Q

What are routed queries? What is the other type?

A

Queries that will fetch data from the single shard.
Scatter and Gather.