gpt4 book ai didi

mongodb - sails 船 : automatically create composite unique index (mongodb)

转载 作者:可可西里 更新时间:2023-11-01 09:12:25 25 4
gpt4 key购买 nike

我的 SailsJS 应用程序中有以下模型,我想在字段“room_name”和“school_id”上添加复合唯一键。

我目前所做的是从 mongo 运行这个命令:

 db.room.ensureIndex({'room_name': 1, 'school_id':1}, {unique: true})

问题 1我做得对吗?

问题 2是否可以修改我的模型,使其自动调用此命令而无需手动修改 mongodb(从 mongo 命令行)?

这是模型

module.exports = {

schema: true,

attributes: {

room_name: {
type: 'string',
required: true
},

school_id: {
type: 'string',
required: true
},

children_count: {
type: 'integer',
required: true
}
}
}

最佳答案

我创建了一个 sails Hook ,为使用 sails-mongo 适配器的模型提供高级索引选项。

支持所有 mongo 索引选项。

https://www.npmjs.com/package/sails-hook-mongoat

关于mongodb - sails 船 : automatically create composite unique index (mongodb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22900809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com