Notion app logo
Struggling to stay organized and effective? Learn to master your time and tasks with the Effectively Notion course!

MongoDB

An overview of MongoDB from my perspective and experience.

2022-07-25

Common Query Patterns

This is a collection built up over time of queries I use and find helpful.

Document Arrays

Find any documents where key smartEvents exist and has an array length that's greater than 0:

{smartEvents: { $exists: true, $not: { $size: 0 } } }

Find any subdocuments of governs and an array key for groups where groups contains an ObjectID of 622b8fe0968213000946ee1e

{"governs.groups":{ $in: [ObjectId('622b8fe0968213000946ee1e')]}}

$elemMatch

$elemMatch is a useful operator for when you want to search an array of subdocuments where a field contains a value. In this example, responses is an array with a document that has a field of content:

{responses: {$elemMatch: {"content": {$regex:"Maybe tolerances could"}}}}

Also note $regex in the query above. This operator lets you search for a regular expression (or a plain string) within a value. This is useful when you don't want to search the whole value of a property in a document.

MongoDB Compass

I use Compass pretty heavily for querying data in MongoDB.

Photo of Chase Adams

Hey, I'm Chase. I help aspiring entrepreneurs and makers turn their ideas into digital products and apps.

Freebies

Vector Arrow Scribbles
banner image for figma vector arrows scribbles

A figma community project of vectorized hand-drawn arrows.

Go To Figma
Vector Line Scribbles
banner for figma vector line scribbles

A figma community project of vectorized hand-drawn lines.

Go To Figma
Vector Shape Scribbles
banner image of figma vector shapes scribbles

A figma community project of vectorized hand-drawn shapes.

Go To Figma

Subscribe to my Newsletter

Every other week I publish the Curiously Crafted newsletter.

In it, I explore the intersection of curiosity and craft: the people who make stuff, what they make and the way they pursue the craft of making.

Online

I'm on almost all social media as @curiouslychase.

The curious logo of Chase Adams: glasses and a bow tie.stay curious.