Insert and Find Operations · MongoDB
Ch 2 · MongoDB CRUD Operations — card 1 of 8.
MongoDB's four basic operations are often called CRUD: Create, Read, Update and Delete. This card covers the first two, creating and reading. You will add documents with insertOne and insertMany, fetch one match with findOne, and get many with find, which returns a cursor you turn into an array with toArray. Filters such as $gt and $lt narrow down the results. Updating and deleting come next, in Card 2.5.
- Insert single and multiple documents
- Query with filters
- Count matching documents
MongoDB provides insertOne, insertMany, findOne, and find for creating and reading documents.
insertOne adds a single document, insertMany adds an array. findOne returns the first match, find returns a cursor (use toArray to get all results). Comparison operators like $gt, $lt filter results.
- insertOne returns insertedId
- insertMany returns insertedIds
- find returns a cursor, not array
- Use $gt, $lt, $gte, $lte, $ne for comparisons