Database IntegrationLesson 62Free preview
MongoDB Insert
This lesson is part of Node.js · All levels
Summary
This lesson teaches how to add documents to MongoDB collection from Node.js using insertOne and insertMany. Every document that is saved automatically has a unique IDs that you can use as a reference. You also see the return of the result (insertedId, insertedCount) to ensure the operation is successful.
Key points
- InsertOne inserts a document and returns it.
- InsertMany inserts many documents; use {ordered: false} so that one failure does not stop the rest.
- Field_id is automated if you don't fill it yourself.
- You can keep documents with different structures in one collection.
- Validation (e.g. duplication of unique index) remains valid and will throw error to await.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.