正如here所解释的那样,将一组新文档批量插入MongoDB集合相当容易:
batch = [{:name => "mongodb"}, {:name => "mongoid"}]
Article.collection.insert(batch)
我不容易找到的是如何检索新创建的ID列表。是否可以编写类似的代码:
batch = [{:name => "mongodb"}, {:name => "mongoid"}]
result = Article.collection.insert(batch)
result[:ids] # not real code
提前致谢!