I am trying to create a two column unique index on the underlying mongodb in a meteor app and having trouble. I can't find anything in the meteor docs. I have tried from the chrome console. I have tried from term and even tried to point mongod at the /db/ dir inside .meteor . I have tried
我试图在流星应用程序中的底层mongodb上创建一个两列唯一索引并遇到麻烦。我在流星文档中找不到任何内容。我试过Chrome控制台。我从学期开始尝试,甚至试图将mongod指向/ db / dir里面的.meteor。我试过了
Collection.ensureIndex({first_id: 1, another_id: 1}, {unique: true});
variations.
Collection.ensureIndex({first_id:1,another_id:1},{unique:true});变化。
I want to be able to prevent duplicate entries on a meteor app mongo collection.
我希望能够防止meteor app mongo集合上的重复条目。
Wondering if anyone has figured this out?
想知道是否有人想出这个?
I answered my own question, what a noob.
我回答了自己的问题,这是一个菜鸟。
I figured it out.
我想到了。
-
Start meteor server
启动流星服务器
-
Open 2nd terminal and type
meteor mongo
打开第二个终端并输入meteor mongo
Then create your index...for example I did these for records of thumbsup and thumbsdown type system.
然后创建你的索引...例如我为thumbsup和thumbsdown类型系统的记录做了这些。
db.thumbsup.ensureIndex({item_id: 1, user_id: 1}, {unique: true})
db.thumbsdown.ensureIndex({item_id: 1, user_id: 1}, {unique: true})
Now, just gotta figure out a bootstrap install setup that creates these when pushed to prod instead of manually.
现在,只需要找出一个引导安装设置,在设置为推送而不是手动时创建这些设置。
4 个解决方案
#1
15
According to the docs "Minimongo currently doesn't have indexes. This will come soon." And looking at the methods available on a Collection, there's no ensureIndex
.
根据文档“Minimongo目前没有索引。这将很快到来。”并且查看Collection上可用的方法,没有ensureIndex。
You can run meteor mongo
for a mongo shell and enable the indexes server-side, but the Collection object still won't know about them. So the app will let you add multiple instances to the Collection cache, while on the server-side the additional inserts will fail silently (errors get written to the output). When you do a hard page refresh, the app will re-sync with server
您可以为mongo shell运行meteor mongo并启用索引服务器端,但Collection对象仍然不会知道它们。因此,应用程序将允许您向Collection缓存添加多个实例,而在服务器端,其他插入将无提示失败(错误将写入输出)。当您进行硬页面刷新时,应用程序将与服务器重新同步
So your best bet for now is probably to do something like:
所以你现在最好的选择可能就是:
var count = MyCollection.find({first_id: 'foo', another_id: 'bar'}).count()
if (count === 0)
MyCollection.insert({first_id: 'foo', another_id: 'bar'});
Which is obviously not ideal, but works ok. You could also enable indexing in mongodb on the server, so even in the case of a race condition you won't actually get duplicate records.
这显然不理想,但工作正常。您还可以在服务器上的mongodb中启用索引,因此即使在竞争条件下,您也不会实际获得重复记录。
#2
31
Collection._ensureIndex(index, options)
Searching inside Meteor source code, I found a bind to ensureIndex called _ensureIndex
. For single-key basic indexes you can follow the example of packages/accounts-base/accounts_server.js
that forces unique usernames on Meteor:
在Meteor源代码中搜索,我找到了一个名为_ensureIndex的ensureIndex绑定。对于单键基本索引,您可以遵循强制在Meteor上使用唯一用户名的packages / accounts-base / accounts_server.js示例:
Meteor.users._ensureIndex('username', {unique: 1, sparse: 1});
For multi-key "compound" indexes:
对于多键“复合”索引:
Collection._ensureIndex({first_id:1, another_id:1}, {unique: 1});
The previous code, when placed on the server side, ensures that indexes are set.
放置在服务器端的先前代码可确保设置索引。
Warning
Notice _ensureIndex implementation warning:
注意_ensureIndex实现警告:
We'll actually design an index API later. For now, we just pass through to Mongo's, but make it synchronous.
我们实际上稍后会设计一个索引API。现在,我们只是传递给Mongo,但让它同步。
#3
3
The Smartpackage aldeed:collection2 supports unique indices, as well as schema-validation. Validation will both occure on server and client (reactivly), so you can react on errors on the client.
Smartpackage aldeed:collection2支持唯一索引以及模式验证。验证将在服务器和客户端上发生(重新激活),因此您可以对客户端上的错误做出反应。
#4
1
Actually why not use upsert on the server with a Meteor.method and you could also send also track it with a ts: // Server Only
实际上为什么不使用Meteor.method在服务器上使用upsert,你也可以使用ts://仅发送服务器来跟踪它
Meteor.methods({
add_only_once = function(id1,id2){
SomeCollection.update(
{first_id:id1,another_id:id2},{$set:{ts:Date.now()}},{upsert:True});
}
});
// Client
Meteor.call('add_only_once',doc1._id, doc2._id);
// actual code running on server
//在服务器上运行的实际代码
if(Meteor.is_server) {
Meteor.methods({
register_code: function (key,monitor) {
Codes.update({key:key},{$set:{ts:Date.now()}},{upsert:true});
}
...
#1
15
According to the docs "Minimongo currently doesn't have indexes. This will come soon." And looking at the methods available on a Collection, there's no ensureIndex
.
根据文档“Minimongo目前没有索引。这将很快到来。”并且查看Collection上可用的方法,没有ensureIndex。
You can run meteor mongo
for a mongo shell and enable the indexes server-side, but the Collection object still won't know about them. So the app will let you add multiple instances to the Collection cache, while on the server-side the additional inserts will fail silently (errors get written to the output). When you do a hard page refresh, the app will re-sync with server
您可以为mongo shell运行meteor mongo并启用索引服务器端,但Collection对象仍然不会知道它们。因此,应用程序将允许您向Collection缓存添加多个实例,而在服务器端,其他插入将无提示失败(错误将写入输出)。当您进行硬页面刷新时,应用程序将与服务器重新同步
So your best bet for now is probably to do something like:
所以你现在最好的选择可能就是:
var count = MyCollection.find({first_id: 'foo', another_id: 'bar'}).count()
if (count === 0)
MyCollection.insert({first_id: 'foo', another_id: 'bar'});
Which is obviously not ideal, but works ok. You could also enable indexing in mongodb on the server, so even in the case of a race condition you won't actually get duplicate records.
这显然不理想,但工作正常。您还可以在服务器上的mongodb中启用索引,因此即使在竞争条件下,您也不会实际获得重复记录。
#2
31
Collection._ensureIndex(index, options)
Searching inside Meteor source code, I found a bind to ensureIndex called _ensureIndex
. For single-key basic indexes you can follow the example of packages/accounts-base/accounts_server.js
that forces unique usernames on Meteor:
在Meteor源代码中搜索,我找到了一个名为_ensureIndex的ensureIndex绑定。对于单键基本索引,您可以遵循强制在Meteor上使用唯一用户名的packages / accounts-base / accounts_server.js示例:
Meteor.users._ensureIndex('username', {unique: 1, sparse: 1});
For multi-key "compound" indexes:
对于多键“复合”索引:
Collection._ensureIndex({first_id:1, another_id:1}, {unique: 1});
The previous code, when placed on the server side, ensures that indexes are set.
放置在服务器端的先前代码可确保设置索引。
Warning
Notice _ensureIndex implementation warning:
注意_ensureIndex实现警告:
We'll actually design an index API later. For now, we just pass through to Mongo's, but make it synchronous.
我们实际上稍后会设计一个索引API。现在,我们只是传递给Mongo,但让它同步。
#3
3
The Smartpackage aldeed:collection2 supports unique indices, as well as schema-validation. Validation will both occure on server and client (reactivly), so you can react on errors on the client.
Smartpackage aldeed:collection2支持唯一索引以及模式验证。验证将在服务器和客户端上发生(重新激活),因此您可以对客户端上的错误做出反应。
#4
1
Actually why not use upsert on the server with a Meteor.method and you could also send also track it with a ts: // Server Only
实际上为什么不使用Meteor.method在服务器上使用upsert,你也可以使用ts://仅发送服务器来跟踪它
Meteor.methods({
add_only_once = function(id1,id2){
SomeCollection.update(
{first_id:id1,another_id:id2},{$set:{ts:Date.now()}},{upsert:True});
}
});
// Client
Meteor.call('add_only_once',doc1._id, doc2._id);
// actual code running on server
//在服务器上运行的实际代码
if(Meteor.is_server) {
Meteor.methods({
register_code: function (key,monitor) {
Codes.update({key:key},{$set:{ts:Date.now()}},{upsert:true});
}
...