[英]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.