Python 3.x 用于检查Mongo DB字段中是否存在python变量的条件。如果已经存在,忽略oherwise推送至DB

Python 3.x 用于检查Mongo DB字段中是否存在python变量的条件。如果已经存在,忽略oherwise推送至DB,python-3.x,pandas,mongodb,flask,Python 3.x,Pandas,Mongodb,Flask,示例记录格式- for file in files: if file and allowed_file(file.filename): templist = [] ... string = ''.join([e[1] for e in sorted(license_info, key=lambda x:x[0])]) ## This is pushed to MongoDB in a field name I

示例记录格式-

for file in files:
        if file and allowed_file(file.filename):
            templist = []
            ...
            string = ''.join([e[1] for e in sorted(license_info, key=lambda x:x[0])]) ## This is pushed to MongoDB in a field name ID.
            templist.append(string)
            currentdate = datetime.datetime.now().date()
            date = currentdate + relativedelta(years=1) ## This is pushed to MongoDB in a field name date.
            templist.append(str(date))
            df.loc[len(df)] = templist
if collection.count_documents({}) == 0: ## If the table is empty I have pushed the dataframe directly in DB.
   collection.insert_many(df.to_dict('records'))
else:
   ## Here is the help needed !!!
   If DB has some data I want to search the string value in the field "ID", if it is present then corresponding "Date" value will be printed, else push the Field ID and date coming from above loop in the DB. This is to avoid duplicacy of data in field "ID".
在上面的示例中,两条记录中的ID值是相同的,我必须避免这种情况,并首先在ID部分的DB中从python循环中的字符串中搜索。如果存在,请打印相应的日期,否则不要将该字符串作为字段值插入ID中,因为它已存在于DB中。 我是Mongo DB的新手,想用Python来做这件事,建议一些事情。。。
提前谢谢

所以只需使用ID查询mongoDB?我是mongoDB的新手,请您介绍一些python中的查询代码。这将是很大的帮助。使用“collection.find_one({“ID”:your_ID_string})”搜索字段“ID”中的字符串值,看看它是否存在。谢谢BOB,它成功了!!同一系列中还有一个问题-if collection.find_One({“ID”:您的_ID_string}):那么如何在if循环中的日期字段中获取相应的日期??请帮忙@RajatKumarSinha用于检查
ID
以及
if
-
集合中的
Date
。查找一个({“ID”:您的ID\u字符串,“Date”:您的日期})
。。。但是如果您想要结果的相应日期,那么您可能可以做如下操作:查找一个({“ID”:您的\u ID\u string})然后
corr\u date=result['date']
——您可以学习mongo+python的基础知识
ObjectId('5f7ebd2e8827d15b764e9402'), 'ID': 'b0862b10-acd2-81e4-2991-086266236905', 'Date': '2021-10-08'}
{'_id': ObjectId('5f7ebd2e8827d15b764e9403'), 'ID': 'b0862b10-acd2-81e4-2991-086266236905', 'Date': '2021-10-08'}