D
D
Dream2021-05-28 18:33:26
Python
Dream, 2021-05-28 18:33:26

How to check collection for existence of _id with specified text?

How to check collection for existence of _id with specified text?

Example:
If name is not in the records (_id) of the collection, then everything is fine, if there is, then we print the message print("The record already exists!")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Romanov, 2021-05-28
@dreameddd

You can check the quantity

if db.test.count_documents({"_id": name})  > 0:
    print("Уже существует")

It is possible to catch an exception when inserting Duplicate something there Error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question