为“每个文档"获取所有集合文档ID的RavenDB.修改 [英] Get all of a collection's documents id's RavenDB for a "per-document" modification

查看:38
本文介绍了为“每个文档"获取所有集合文档ID的RavenDB.修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试更新ravendb数据库中的文档.问题是我有一种更新一个文档的方法,但是它以文档ID为参数.因此,我使用的是python:pyravenDB作为接口.

I'm currently trying to update my documents in a ravendb DB. The issue is that i have a method that updates one document, yet it takes as a parameter the id of the doc. I'm using python, therefore : pyravenDB as an interface.

方法如下:

def updateDocument(self,id,newAttribute)

        with self.store.open_session() as session:
            doc = session.load(id)
            doc.newAttribute= newAttribute
            session.save_changes()

我的想法是,我将使用带有目标集合的所有ID的简单for循环,并调用updateDocument方法.

My idea is the i will use a simple for loop with all the id's of the targeted collection and call the updateDocument method.

我认为有一个updatebyindex方法,但是我不知道如何使它适应我的用例.

I think there is an updatebyindex method, but I don't get how to adapt it to my usecase.

我怎么能得到这个?

谢谢!

推荐答案

像maqduni所说的 update_by_index 是您要使用的方法.只需创建一个索引即可索引所需的文档.如果您遇到麻烦,可以尝试查询所需的文档,然后ravendb将为您创建自动索引.创建索引后,只需使用 index_name query 调用 update_by_index (只需确保索引不是陈旧的)

Like maqduni said update_by_index is the method you want to use. just create an index that will index the documents you want. if you getting trouble with that you can just try to query on the documents you want and then ravendb will create auto index for you. after your index creation just simply call update_by_index with the index_name and the query (Just make sure the index is not stale)

您的代码需要看起来像这样:

your code need to look something like this:

from pyravendb.data.indexes import IndexQuery
from pyravendb.data.patches import ScriptedPatchRequest
   self.store.database_commands.update_by_index(index_name="YOUR_INDEX_NAME",
        query=IndexQuery(query="TAG:collection_name;"),
        scripted_patch=ScriptedPatchRequest("this.Attribute = newAttribute;"))

IndexQuery 中的查询是lucene语法,在示例TAG中,索引是我的所有集合名称. scripted_pa​​tch 采用js语法,这是将在您查询的每个文档上运行的脚本.

the query in IndexQuery is lucene syntax in the example TAG in the index is all my collection names. the scripted_patch is in js syntax and this is the script that will run on each document you query on.

我将尝试解释两者之间的区别:

I will try to explain the different between the two:

get_index 方法将为您提供有关索引的信息,响应为 IndexDefinition .

the get_index method will give you information about the index the response is the IndexDefinition.

update_by_index 是一项漫长的任务,因此为什么只获得 operation_id 的原因需要等待直到完成.(将在下一个pyravendb版本中为其提供功能).此操作不会给您打补丁的文档.新功能将为您提供有关该过程的信息.

the update_by_index is a long task operation and that why you only get the operation_id you need to wait until it is finished. (will make a feature for that in the next pyravendb version). this operation won't give you the documents that patched. the new feature will give you information about the process.

page_size 仅用于查询结果,而不用于索引操作

the page_size is for query results only not for a index operation

这篇关于为“每个文档"获取所有集合文档ID的RavenDB.修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆