仅当字段不为空时才将字段设置为索引
Setting a field as index only if it is not empty
我有一个包含两个键的结构,如下所示:
{
"cannot_be_empty": "hello",
"can_be_empty": "world"
}
现在我将索引设置为 can_be_empty
和 cannot_be_empty
的组合。
db.collection.Indexes.CreateOne(IndexModel{{Keys: bson.D{
{Key: "cannot_be_empty", Value: 1},
{Key: "can_be_empty", Value: 1},
}})
但是,我想让 can_be_empty
在所有字段中都必须是唯一的,除非它是一个空字符串。有没有办法做到这一点?我认为 https://docs.mongodb.com/manual/core/index-partial/ 可能是要走的路,但我不太确定它是如何工作的。
谢谢!
更新:我发现 $ne
不起作用,所以我尝试了这个,目前我的测试仍然失败,但我会尝试找出原因。
// Set partial index option
myPartialIndex := options.Index().SetPartialFilterExpression(bson.D{{
Key: "can_be_empty",
Value: bson.D{{Key: "$gt", Value: ""}},
}})
// Set up all indexes
indexes := []mongo.IndexModel{
{Keys: bson.D{{Key: "cannot_be_empty", Value: 1}, {Key: "can_be_empty", Value: 1}}},
{Keys: bson.D{{Key: "can_be_empty", Value: 1}}, Options: myPartialIndex},
}
// Create index, no error
db.collection.Indexes().CreateMany(ctx, indexes)
更新 2:我不知道我必须添加一个独特的选项才能让它工作。无论如何,我还需要 can_be_empty
和 cannot_be_empty
的复合键是唯一的。
最终更新:我接受了答案,但也会 post 我自己的 Golang 版本。
myUniqueIndex := options.Index().SetUnique(true)
myPartialIndex := options.Index().SetPartialFilterExpression(bson.D{{
Key: "can_be_empty",
Value: bson.D{{Key: "$gt", Value: ""}},
}})
// Set up all indexes
indexes := []mongo.IndexModel{
{Keys: bson.D{{Key: "cannot_be_empty", Value: 1}, {Key: "can_be_empty", Value: 1}}, Options: myUniqueIndex},
{Keys: bson.D{{Key: "can_be_empty", Value: 1}}, Options: myPartialIndex},
}
// Create index, no error
db.collection.Indexes().CreateMany(ctx, indexes)
您可以使用 $gt
空字符串。
db.collection.createIndex(
{ can_be_empty: 1, cannot_be_empty: 1 },
{ unique: true, partialFilterExpression: { "can_be_empty": { "$gt": "" } } }
)
我有一个包含两个键的结构,如下所示:
{
"cannot_be_empty": "hello",
"can_be_empty": "world"
}
现在我将索引设置为 can_be_empty
和 cannot_be_empty
的组合。
db.collection.Indexes.CreateOne(IndexModel{{Keys: bson.D{
{Key: "cannot_be_empty", Value: 1},
{Key: "can_be_empty", Value: 1},
}})
但是,我想让 can_be_empty
在所有字段中都必须是唯一的,除非它是一个空字符串。有没有办法做到这一点?我认为 https://docs.mongodb.com/manual/core/index-partial/ 可能是要走的路,但我不太确定它是如何工作的。
谢谢!
更新:我发现 $ne
不起作用,所以我尝试了这个,目前我的测试仍然失败,但我会尝试找出原因。
// Set partial index option
myPartialIndex := options.Index().SetPartialFilterExpression(bson.D{{
Key: "can_be_empty",
Value: bson.D{{Key: "$gt", Value: ""}},
}})
// Set up all indexes
indexes := []mongo.IndexModel{
{Keys: bson.D{{Key: "cannot_be_empty", Value: 1}, {Key: "can_be_empty", Value: 1}}},
{Keys: bson.D{{Key: "can_be_empty", Value: 1}}, Options: myPartialIndex},
}
// Create index, no error
db.collection.Indexes().CreateMany(ctx, indexes)
更新 2:我不知道我必须添加一个独特的选项才能让它工作。无论如何,我还需要 can_be_empty
和 cannot_be_empty
的复合键是唯一的。
最终更新:我接受了答案,但也会 post 我自己的 Golang 版本。
myUniqueIndex := options.Index().SetUnique(true)
myPartialIndex := options.Index().SetPartialFilterExpression(bson.D{{
Key: "can_be_empty",
Value: bson.D{{Key: "$gt", Value: ""}},
}})
// Set up all indexes
indexes := []mongo.IndexModel{
{Keys: bson.D{{Key: "cannot_be_empty", Value: 1}, {Key: "can_be_empty", Value: 1}}, Options: myUniqueIndex},
{Keys: bson.D{{Key: "can_be_empty", Value: 1}}, Options: myPartialIndex},
}
// Create index, no error
db.collection.Indexes().CreateMany(ctx, indexes)
您可以使用 $gt
空字符串。
db.collection.createIndex(
{ can_be_empty: 1, cannot_be_empty: 1 },
{ unique: true, partialFilterExpression: { "can_be_empty": { "$gt": "" } } }
)