@@ -60,6 +60,23 @@ The following example inserts a document into the ``restaurants`` collection:
60
60
61
61
sample_restaurants.restaurants.insert_one({"name" : "Mongo's Burgers"})
62
62
63
+ You can also pass an instance of a custom class to the ``insert_one()`` method.
64
+ This provides additional type safety if you're using a type-checking
65
+ tool. The instance you pass must inherit from the ``TypedDict`` class.
66
+
67
+ .. include:: /includes/type-hints/typeddict-availability.rst
68
+
69
+ The following example passes an instance of the ``Restaurant`` class to the ``insert_one()``
70
+ method for added type safety:
71
+
72
+ .. code-block:: python
73
+ :copyable: true
74
+
75
+ class Restaurant(TypedDict):
76
+ name: str
77
+
78
+ sample_restaurants.restaurants.insert_one(Movie(name="Mongo's Burgers")
79
+
63
80
Insert Multiple Documents
64
81
-------------------------
65
82
@@ -78,6 +95,28 @@ The following example inserts a list of documents into the ``restaurants`` colle
78
95
79
96
sample_restaurants.restaurants.insert_many(document_list)
80
97
98
+ You can also pass a list of instances of a custom class to the ``insert_many()`` method.
99
+ This provides additional type safety if you're using a type-checking
100
+ tool. The instances you pass must inherit from the ``TypedDict`` class.
101
+
102
+ .. include:: /includes/type-hints/typeddict-availability.rst
103
+
104
+ The following example calls the ``insert_many()`` method and passes a list that contains
105
+ instances of the ``Restaurant`` class. This adds type safety to the insert operation.
106
+
107
+ .. code-block:: python
108
+ :copyable: true
109
+
110
+ class Restaurant(TypedDict):
111
+ name: str
112
+
113
+ document_list = [
114
+ Restaurant(name="Mongo's Burgers"),
115
+ Restaurant(name="Mongo's Pizza")
116
+ ]
117
+
118
+ sample_restaurants.restaurants.insert_many(document_list)
119
+
81
120
Modify Insert Behavior
82
121
----------------------
83
122
@@ -140,41 +179,6 @@ document-level validation.
140
179
141
180
sample_restaurants.restaurants.insert_many(document_list, bypass_document_validation = True)
142
181
143
- Type Hints
144
- ----------
145
-
146
- .. include:: /includes/type-hints/intro.rst
147
-
148
- When you call the ``insert_one()`` or ``insert_many()`` method, you can pass one or more
149
- instances of a custom class that represents the documents in the collection.
150
-
151
- To create
152
- a custom type, define a class that inherits from the ``TypedDict`` class. In the class,
153
- add a property for each field in the document.
154
-
155
- After you define the class, you can insert instances of the class. The following example
156
- defines a ``Movie`` class to represent documents from the
157
- ``sample_mflix.movies`` collection.
158
- Each ``Movie`` object contains two key-value pairs: ``name``, a string key with a string
159
- value, and ``year``, a string key with an integer value.
160
- The code then uses the ``insert_one()`` method to insert a ``Movie`` object.
161
-
162
- .. code-block:: python
163
- :emphasize-lines: 10
164
-
165
- from typing import TypedDict
166
-
167
- class Movie(TypedDict):
168
- name: str
169
- year: int
170
-
171
- client: MongoClient = MongoClient()
172
- database = client["test_database"]
173
- collection: Collection[Movie] = database["test_collection"]
174
- inserted = collection.insert_one(Movie(name="Jurassic Park", year=1993))
175
-
176
- .. include:: /includes/type-hints/tip-more-info.rst
177
-
178
182
Troubleshooting
179
183
---------------
180
184
@@ -308,6 +312,7 @@ The following code example shows how to implement this solution:
308
312
The ``NotRequired`` class is available only in Python 3.11 and later.
309
313
To use ``NotRequired`` in earlier versions of Python, install the ``typing_extensions``
310
314
package instead.
315
+
311
316
Additional Information
312
317
----------------------
313
318
0 commit comments