RavenDB 查询 ID

发布于 2024-11-01 19:07:07 字数 1680 浏览 3 评论 0原文

我有一个名为 Post 的模型,其中有一个名为 Slug 的字段。我已经用属性 [ID] 标记了该字段,并更改了 RavenDB 中的 FindIdentityProperty 约定,以便它将用 [ID] 标记的属性视为文档的身份。

现在我想获取所有包含以字符 G 开头的 slugs 的帖子,我是否需要创建一个像这样的索引:

from Post in docs.Posts
select new { Slug = Post.Slug }

或者像这样

from Post in docs.Posts
select new { Id = Post.Id }

,或者我是否使用 RavenDB 内部应该有的索引,因为我正在查询 ID。 据我所知,RavenDB 在某处索引了 ID 属性,以便可以使用 Session.Load("SomeID") 。

第一个选项可以在 C# 代码中定义为 AbstractIndexCreationTask,因为 C# 理解 Slug 是 Post 的一个字段,但不起作用,因为它是“字符串”化到 RavenDB 中,如下所示:(当索引创建发生时)

from Post in docs.Posts
select new { Slug = Post.Slug }

变为

docs.Posts
    .Select(Post => new {Slug = Post.Slug })

您可以看到创建索引后查看 SL-UI 时会出现此情况。

这确实不起作用,因为(如数据库的 SL-UI 中所示)帖子的 JSON 表示,没有像 "Slug":"Some- 这样的名称-值对蛞蝓”。由于它是 ID,因此显示在文档上方,并且在 @metadata 中,它可以被视为 _document_id 的值。

后者确实有效,但仅当定义为一个字符串,就像 C# 中的这样,

public class Post_ById : AbstractIndexCreationTask
    {
        public override IndexDefinition CreateIndexDefinition()
        {
            return new IndexDefinition
                       {
                           Map = "from Post in docs.Posts select new { Id = Post.Id}"
                       };
        }
    }

我怀疑当 Raven 获取包含字符串“Id”的查询时,它会自动将其视为是对[@metadata][_document_id]

这里没有 LINQ 的好处 - 看起来非常脆弱,无法在域名重构中幸存。 Id 不是 Post 的字段,但这个索引确实允许我做我想做的事情,即“获取所有具有以字符 G 开头的 slugs 的帖子” 。当数据库应该已经有这样的索引时,我讨厌为 Id 定义索引。

I have a Model called Post, which has a field called Slug. I've marked this field with the attribute [ID] and changed the FindIdentityProperty Convention in RavenDB such that it considers the property marked with [ID] as the Identity of the document.

Now I want to fetch all posts that have slugs that start with the character G, do I need to create an index like this :

from Post in docs.Posts
select new { Slug = Post.Slug }

or like this

from Post in docs.Posts
select new { Id = Post.Id }

or do I use the index that RavenDB internally should have since I'm querying on ID.
I understand that RavenDB has the ID property indexed somewhere so that a Session.Load<Post>("SomeID") is possible.

The 1st option can be defined in code in C# as an AbstractIndexCreationTask because C# understands that Slug is a field of Post, but does not work as it is "string"ified into RavenDB like this : (when Index Creation happens)

from Post in docs.Posts
select new { Slug = Post.Slug }

becomes

docs.Posts
    .Select(Post => new {Slug = Post.Slug })

You can see this when you look in the SL-UI, after indexes are created.

And this does NOT work because ( as shown in the SL-UI of the DB ) the JSON representation of the Post, there is no name-value pair like "Slug":"Some-Slug". Since it's the ID it is shown above the document, and in the @metadata it can be seen as the value of _document_id

The latter one does work, but only when defined as a string, like this in C#

public class Post_ById : AbstractIndexCreationTask
    {
        public override IndexDefinition CreateIndexDefinition()
        {
            return new IndexDefinition
                       {
                           Map = "from Post in docs.Posts select new { Id = Post.Id}"
                       };
        }
    }

I suspect when Raven gets queries that contain the string "Id", it automatically takes it to mean that it's a query on [@metadata][_document_id].

No LINQ goodness here - looks very brittle, won't survive a refactor of the domain name. Id is not a field of Post, but this index did allow me to do what I wanted, namely, "fetch all posts that have slugs that start with the character G". And I hate defining indexes for Ids when DB should already have such an index.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖正好 2024-11-08 19:07:07

嗯,我们实际上也在 AbstractIndexCreationTask 中使用相同的约定,所以这应该可以工作。
无论如何,文档的 id 始终可以使用 __document_id 访问

Hm, we actually use the same conventions in AbstractIndexCreationTask as well, so that should work.
At any rate, the id of the document can always be accessed using __document_id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文