MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / get_collection

Method get_collection

pymongo/synchronous/database.py:252–304  ·  view source on GitHub ↗

Get a :class:`~pymongo.collection.Collection` with the given name and options. Useful for creating a :class:`~pymongo.collection.Collection` with different codec options, read preference, and/or write concern from this :class:`Database`. >>> db.read_prefer

(
        self,
        name: str,
        codec_options: Optional[CodecOptions[_DocumentTypeArg]] = None,
        read_preference: Optional[_ServerMode] = None,
        write_concern: Optional[WriteConcern] = None,
        read_concern: Optional[ReadConcern] = None,
    )

Source from the content-addressed store, hash-verified

250 return Collection(self, name)
251
252 def get_collection(
253 self,
254 name: str,
255 codec_options: Optional[CodecOptions[_DocumentTypeArg]] = None,
256 read_preference: Optional[_ServerMode] = None,
257 write_concern: Optional[WriteConcern] = None,
258 read_concern: Optional[ReadConcern] = None,
259 ) -> Collection[_DocumentType]:
260 """Get a :class:`~pymongo.collection.Collection` with the given name
261 and options.
262
263 Useful for creating a :class:`~pymongo.collection.Collection` with
264 different codec options, read preference, and/or write concern from
265 this :class:`Database`.
266
267 >>> db.read_preference
268 Primary()
269 >>> coll1 = db.test
270 >>> coll1.read_preference
271 Primary()
272 >>> from pymongo import ReadPreference
273 >>> coll2 = db.get_collection(
274 ... 'test', read_preference=ReadPreference.SECONDARY)
275 >>> coll2.read_preference
276 Secondary(tag_sets=None)
277
278 :param name: The name of the collection - a string.
279 :param codec_options: An instance of
280 :class:`~bson.codec_options.CodecOptions`. If ``None`` (the
281 default) the :attr:`codec_options` of this :class:`Database` is
282 used.
283 :param read_preference: The read preference to use. If
284 ``None`` (the default) the :attr:`read_preference` of this
285 :class:`Database` is used. See :mod:`~pymongo.read_preferences`
286 for options.
287 :param write_concern: An instance of
288 :class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
289 default) the :attr:`write_concern` of this :class:`Database` is
290 used.
291 :param read_concern: An instance of
292 :class:`~pymongo.read_concern.ReadConcern`. If ``None`` (the
293 default) the :attr:`read_concern` of this :class:`Database` is
294 used.
295 """
296 return Collection(
297 self,
298 name,
299 False,
300 codec_options,
301 read_preference,
302 write_concern,
303 read_concern,
304 )
305
306 def _get_encrypted_fields(
307 self, kwargs: Mapping[str, Any], coll_name: str, ask_db: bool

Calls 1

CollectionClass · 0.90