Ste*_*non 26 javascript mongodb meteor
我试图在流星应用程序中的底层mongodb上创建一个两列唯一索引并遇到麻烦.我在流星文档中找不到任何内容.我试过Chrome控制台.我从学期开始尝试,甚至试图将mongod指向/ db/dir里面的.meteor.我试过了
Collection.ensureIndex({first_id: 1, another_id: 1}, {unique: true}); 变化.
我希望能够防止meteor app mongo集合上的重复条目.
想知道是否有人想出这个?
我回答了自己的问题,这是一个菜鸟.
我想到了.
启动流星服务器
打开第二个终端并输入 meteor mongo
然后创建你的索引...例如我为thumbsup和thumbsdown类型系统的记录做了这些.
db.thumbsup.ensureIndex({item_id: 1, user_id: 1}, {unique: true})
db.thumbsdown.ensureIndex({item_id: 1, user_id: 1}, {unique: true})
Run Code Online (Sandbox Code Playgroud)
现在,只需要找出一个引导安装设置,在设置为推送而不是手动时创建这些设置.
zVi*_*tor 31
在Meteor源代码中搜索,我发现了一个对ensureIndex进行调用的绑定_ensureIndex.对于单键基本索引,您可以按照packages/accounts-base/accounts_server.jsMeteor上强制唯一用户名的示例:
Meteor.users._ensureIndex('username', {unique: 1, sparse: 1});
Run Code Online (Sandbox Code Playgroud)
对于多键"复合"索引:
Collection._ensureIndex({first_id:1, another_id:1}, {unique: 1});
Run Code Online (Sandbox Code Playgroud)
放置在服务器端的先前代码可确保设置索引.
注意_ensureIndex实现警告:
我们实际上稍后会设计一个索引API.现在,我们只是传递给Mongo,但让它同步.
dan*_*nny 15
根据文档 "Minimongo目前没有索引.这将很快到来." 看看Collection上可用的方法,没有ensureIndex.
您可以运行meteor mongomongo shell并启用索引服务器端,但Collection对象仍然不会知道它们.因此,应用程序将允许您向Collection缓存添加多个实例,而在服务器端,其他插入将无提示失败(错误将写入输出).当您执行硬页面刷新时,应用程序将与服务器重新同步
所以你现在最好的选择可能就是:
var count = MyCollection.find({first_id: 'foo', another_id: 'bar'}).count()
if (count === 0)
MyCollection.insert({first_id: 'foo', another_id: 'bar'});
Run Code Online (Sandbox Code Playgroud)
这显然不理想,但工作正常.您还可以在服务器上的mongodb中启用索引,因此即使在竞争条件下,您也不会实际获得重复记录.
| 归档时间: |
|
| 查看次数: |
8471 次 |
| 最近记录: |