如何在数组.NET驱动程序中的项的属性上创建MongoDB MultiKey索引 [英] How to create MongoDB MultiKey index on attribute of items in an array .NET Driver

查看:373
本文介绍了如何在数组.NET驱动程序中的项的属性上创建MongoDB MultiKey索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含项目的MongoDB集合foos,每个项目都有一个bars数组。也就是说,foo具有以下架构:

I have a MongoDB collection "foos" containing items which each have an array of "bars". That is, "foo" has the following schema:

{
    "id": UUID
    "name": string
    ...
    "bars": [
        "id": UUID
        "key": string
        ...
    ]
}

我需要使用MongoDB C#在name和bar.key上创建索引.NET Mongo驱动程序。

I need to create an index on name and bar.key using the MongoDB C# .NET Mongo driver.

我认为我可以使用Linq Select函数执行以下操作:

I presumed I could use a Linq Select function to do this as follows:

Indexes.Add(Context.Collection<FooDocument>().Indexes.CreateOne(
    Builders<FooDocument>.IndexKeys
        .Descending(x => x.Bars.Select(y => y.Key))));

但是这会导致InvalidOperationException:

However this results in an InvalidOperationException:


System.InvalidOperationException:'无法确定x => x.Bars.Select(y => y.Id)的序列化信息。'

System.InvalidOperationException: 'Unable to determine the serialization information for x => x.Bars.Select(y => y.Id).'

关于MultiKey索引的Mongo文档显示如何使用简单的点表示法创建这样的索引,即

The Mongo documentation on MultiKey indexes shows how to create such an index using simple dot notation, i.e.

db.foos.createIndex( { "name": 1, "bars.key": 1 } )

然而 MongoDB驱动程序文档似乎建议使用Linq函数,因为我正在做的是正确的。

However the MongoDB driver documentation seems to suggest that using a Linq function as I'm doing is correct.

如何使用MongoDB .NET驱动程序在我的集合上创建多键索引,最好使用Linq函数?

推荐答案

这是一个如何使用C#

var indexDefinition = Builders<FooDocument>.IndexKeys.Combine(
    Builders<FooDocument>.IndexKeys.Ascending(f => f.Key1),
    Builders<FooDocument>.IndexKeys.Ascending(f => f.Key2));

await collection.Indexes.CreateOneAsync(indexDefinition); 

更新

关于数组中的索引,我能找到的最接近的是在构建索引键时使用-1作为索引。据我所知,github源代码是构建查询时的有效选项。

Regarding index within the array, closest what i was able to find is to use "-1" as index whene you building your index key. As i understand from github source code is is a valid option in case of building queries.

var indexDefinition = Builders<FooDocument>.IndexKeys.Combine(
    Builders<FooDocument>.IndexKeys.Ascending(f => f.Key1),
    Builders<FooDocument>.IndexKeys.Ascending(f => f.Key2[-1].Key));

await collection.Indexes.CreateOneAsync(indexDefinition); 

- 1是side mongodb C#驱动程序中的硬编码常量,表示$(证明)。因此,此代码将尝试创建索引:

"-1" is a hardcoded constant in side mongodb C# drivers which means "$" (proof). So this code would try to create index:

{ "Key1": 1, "Key2.$.Key": 1 }

这对从数据库查询信息很好,但不允许(将引发异常索引键包含非法字段名称:字段名称以$开头,以便在索引中使用。所以我认为应该在mongodb驱动程序中进行更改以使其正常工作。像-2这样的东西意味着空操作符。在这种情况下,我们可以使用

which is fine for querying info from database, but not allowed (will throw an exception "Index key contains an illegal field name: field name starts with '$'") to use in indexes. So i assume it should be changed in mongodb drivers to make it work. Something like "-2" means empty operator. In that case we could use

var indexDefinition = Builders<FooDocument>.IndexKeys.Combine(
    Builders<FooDocument>.IndexKeys.Ascending(f => f.Key1),
    Builders<FooDocument>.IndexKeys.Ascending(f => f.Key2[-2].Key));

await collection.Indexes.CreateOneAsync(indexDefinition); 

会生成如下索引:

{ "Key1": 1, "Key2.Key": 1 }

所以基本上我认为现在不可能在不改变mongo C#驱动程序的情况下使用纯Linq构建索引。

所以我认为你唯一的选择就是这样,仍然是C#但没有Linq

So i think your only option do like this, still C# but without Linq

await collection.Indexes.CreateOneAsync(new BsonDocument {{"name", 1}, {"bars.key", 1}});

这篇关于如何在数组.NET驱动程序中的项的属性上创建MongoDB MultiKey索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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