简体   繁体   English

如果集合中的前一个文档包含某个值,我如何不创建新文档?

[英]How do I not create a new document if the previous document in the collection contains a certain value?

I am using the python library to programmatically create documents in a collection as such:我正在使用 python 库以编程方式在集合中创建文档,如下所示:

user = client.query(q.create(q.collection("my_collection"), {
             "data": {
                     "UTC_datetime": str(datetime.now(pytz.UTC)),
                     "item_one": str(value_one),
                     "item_two": str(value_two),
                     "item_three": str(value_three)
         }
     }))

Upon certain conditions being met the python app executes again.在满足某些条件后,python 应用程序将再次执行。

If item_two on the next app execution has the same value again I do not want a new document to be created.如果下一个应用程序执行中的item_two再次具有相同的值,我希望创建新文档。

How do I craft the above query to perform this?如何制作上述查询来执行此操作?

Currently, I am reading the previous document, extracting the value from item_two and performing an if/else statement to either proceed to store a new document or sys.exit() .目前,我正在阅读上一个文档,从item_two中提取值并执行 if/else 语句以继续存储新文档或sys.exit()

I'm positive there is a more elegant solution that is based within Fauna's logic instead of Python's, however, I have not been able to achieve this.我很肯定有一个更优雅的解决方案,它基于 Fauna 的逻辑而不是 Python 的逻辑,但是,我无法实现这一点。

You can create a unique index ( https://docs.fauna.com/fauna/current/api/fql/indexes ) for item_two to ensure that duplicates are not possible.您可以为 item_two 创建一个唯一索引 ( https://docs.fauna.com/fauna/current/api/fql/indexes ) 以确保不可能出现重复。 You may also want to an upsert implementation您可能还想要一个 upsert 实现

https://forums.fauna.com/t/multi-document-upsert/488/3 https://forums.fauna.com/t/multi-document-upsert/488/3

https://forums.fauna.com/t/does-fauna-supports-upserts/208 https://forums.fauna.com/t/does-fauna-supports-upserts/208

q.If(
   q.Exists(q.Match(q.Index('unique_item_two'), str(value_two))), 
   q.Update(...),
   q.Create(...)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中,如何为 arangodb 创建边缘集合和文档? - In Python, how do I create an edge collection and document for an arangodb? 根据MongoDB集合中先前记录的匹配更新新的文档字段值 - Update new document field value based on match of previous records in MongoDB collection 如何在python中创建一个xml文档 - How do I create an xml document in python 如何在tkinter python的文本文档中保存之前的输入 - How do I save the previous input in the text document in tkinter python 如何使用 pymongo 连接到现有的文档集合/数据库? - How do I use pymongo to connect to an existing document collection/db? 如何将元素添加到集合中,然后在此文档中创建子集合? - How to add element into collection and then create subcollection in this document? 如何在MongoDB中更新整个集合,而不是逐个文档更新? - How can I update a whole collection in MongoDB and not document by document? 如何使用熊猫创建响应式Excel文档标题? - How do I create a responsive excel document title using pandas? 如何使用 Gtk 创建和打印文档(到打印机)? - How do I create and print a document (to a printer) using Gtk? 如何在不更改原始文档的情况下修改 stream 中的文档 - How do I modify a document in a stream without changing the original document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM