如果至少存在一个重复项,则 Mongoid 会忽略 collection.insert [英] Mongoid ignores collection.insert if at least one duplicate exists
问题描述
我有一个唯一的索引设置,drop_dup: true
index({ key1: 1, key2: 1 }, { unique: true, drop_dups: true })
插入多条记录时,我希望非重复成功,类似于MySQL的
INSERT IGNORE INTO table ...
... 甚至 INSERT INTO table ... ON DUPLICATE KEY UPDATE
所以如果我有记录:
Model.collection.insert({'key1'=>'not-unique', 'key2'=>'boo'})
似乎下面的调用没有做任何事情.
Model.collection.insert(
{'key1'='not-unique', 'key2'=>'boo'},
{'key1'=>'im-unique', 'key2'=>'me-too'}
)
有没有办法至少在第二次调用时插入 {'key1'=>'im-unique', 'key2'=>'me-too'}
?>
谢谢!
看来您正在寻找带有 continue_on_error 的插入批处理而不是我认为您已经了解的单个文档更新/更新.
http://api.mongodb.org/ruby/current/Mongo/Collection.html#insert-instance_method
使用 10gen 驱动程序,与用户模型的示例等效:
宝石文件
gem 'mongo'宝石'bson_ext'
test/unit/user_test.rb(摘录)
col = Mongo::Connection.new['sandbox_test']['users']col.insert({'key1'=>'not-unique', 'key2'=>'boo'})assert_equal(1, User.count)col.insert([{'key1'=>'not-unique', 'key2'=>'boo'},{'key1'=>'im-unique', 'key2'=>'me-too'}], :continue_on_error => true)assert_equal(2, User.count)
以上在一个简单的单元测试中有效.
Mongoid 3 使用 Moped 驱动程序而不是 10gen 驱动程序.在您使用的 1.1.6 gem 中,轻便摩托车不支持选项/标志,但 Durran 于 7 月 28 日左右在 github 中向插入方法添加了标志.
https://github.com/mongoid/moped/commit/2c7d6cded23f64bdae432f64bdae92a/p> https://github.com/mongoid/moped/commit/f8157b43ef0e135a8c/p> 从带有轻便摩托车 1.2.0 的 mongoid 3.0.3 开始,以下带有 continue_on_error 的插入批处理有效. 注意批量插入的insert方法的主要参数是一个Array对象用方括号括起来 - 您的帖子中缺少方括号. 希望这会有所帮助. I have a unique index setup, with When inserting multiple records, I would like the non-duplicates to succeed, similar to MySQL's .. or even So if I have a record: It seems that the following call doesn't do anything. Is there a way to at least insert Thanks! It appears that you are looking for insert batch with continue_on_error
and not single document update/upsert of which I think you are already informed. http://api.mongodb.org/ruby/current/Mongo/Collection.html#insert-instance_method With the 10gen driver, an equivalent of your example with a User model would be: Gemfile test/unit/user_test.rb (excerpt) The above works in a simple unit test. Mongoid 3 uses the Moped driver instead of the 10gen driver.
In the 1.1.6 gem that you are using, moped does not support options / flags,
but Durran added flags to the insert method circa July 28 in github. https://github.com/mongoid/moped/commit/2c7d6cded23f64f436fd9e992ddc98bbb7bbdaae https://github.com/mongoid/moped/commit/f8157b43ef0e13da85dbfcb7a6dbebfa1fc8735c As of mongoid 3.0.3 with moped 1.2.0, the following insert batch with continue_on_error works. Note that the primary parameter to the insert method for batch insert is an Array object
enclosed in square brackets - the square brackets are missing from your post. Hope that this helps. 这篇关于如果至少存在一个重复项,则 Mongoid 会忽略 collection.insert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Model.collection.insert([{'key1'=>'not-unique', 'key2'=>'boo'},{'key1'=>'im-unique','key2'=>'me-too'}], [:continue_on_error])
drop_dup: true
index({ key1: 1, key2: 1 }, { unique: true, drop_dups: true })
INSERT IGNORE INTO table ...
INSERT INTO table ... ON DUPLICATE KEY UPDATE
Model.collection.insert({'key1'=>'not-unique', 'key2'=>'boo'})
Model.collection.insert(
{'key1'=>'not-unique', 'key2'=>'boo'},
{'key1'=>'im-unique', 'key2'=>'me-too'}
)
{'key1'=>'im-unique', 'key2'=>'me-too'}
on the 2nd call?gem 'mongo'
gem 'bson_ext'
col = Mongo::Connection.new['sandbox_test']['users']
col.insert({'key1'=>'not-unique', 'key2'=>'boo'})
assert_equal(1, User.count)
col.insert([{'key1'=>'not-unique', 'key2'=>'boo'},{'key1'=>'im-unique', 'key2'=>'me-too'}], :continue_on_error => true)
assert_equal(2, User.count)
Model.collection.insert([{'key1'=>'not-unique', 'key2'=>'boo'},{'key1'=>'im-unique', 'key2'=>'me-too'}], [:continue_on_error])