Chase AdamsChase Adams
AboutContentPromptsPlayground

MongoDB

Essays and updates on product, engineering, and AI by Chase Adams.

1 minute read

Common Query Patterns

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

  • update or add a property to subdocuments of all found documents (MongoDB)

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.

First Cohort
No Coding Experience Required

Build Your Website with AI—No Code Required

Learn to create and deploy professional websites using ChatGPT and Claude. Go from complete beginner to confident website builder.

Start Building Today

Post Details

Published
Jul 23, 2022
Category
Engineering
Share
Ask ChatGPT
Ask Claude

Latest Posts

Joining Rocket Money as VP of AI Engineering

Joining Rocket Money as VP of AI Engineering

Leading AI engineering at Rocket Money to write the playbook for how engineering teams operate in the AI era

AboutAI Workflow SpecContentStacksNewsletterPromptsRSS

Ask me anything

Loading...

Hi! Ask me anything about Chase's work.

I can answer questions based on his blog posts and articles.

Experimental: This chat is a side project I work on in my free time. Responses may vary in quality and accuracy.