LiteDB多列/字段ID

问题描述 投票:0回答:1

我有一个有多个字段的类。让我们举个例子说:

class MyClass {
    public int x {get; set;}
    public int y {get; set;}
    public int z {get; set;}
}

我想在该类中使用LiteDB,并使用x和y作为键。例如:

MyClass{x: 0, y: 0, z:0}
MyClass{x: 0, y: 1, z:0}

将被视为我的LiteDB数据库中的2个不同条目。

所以我基本上想要这样的东西:

var mapper = BsonMapper.Global;
mapper
    .Entity<MyClass>()
    .Id(c => new {c.x, c.y});
var db = new LiteDatabase("PATH_HERE", mapper);

显然,Id(c => new {c.x,c.y});不起作用。我也试过了

.Id(c => c.x)
.Id(c => c.y);

但它也不起作用。我试过在LiteDB文档中寻找解决方案,但无法找到任何东西。这可能吗?如果是这样,我很想了解如何实现我的目标。

谢谢!

c# nosql bson litedb
1个回答
1
投票

BsonDocument中,_id必须是单个值,没有复合键。但是这个值可以是另一个文档。所以,你可以使用这个:

class MyId {
    public int x { get; set; }
    public int y { get; set; }
}

class MyClass {
    public MyId Id { get; set; }
    public int y { get; set; }
}

现在你可以找到使用

col.Find(x => x.Id = new Id { x = 0, y = 1 })
© www.soinside.com 2019 - 2024. All rights reserved.