繁体   English   中英

使用Java和Spring数据在mongodb中批量插入仅插入一个文档

[英]bulk insert in mongodb with Java and Spring data inserts only one document

我正在尝试对使用Spring数据定义的对象列表执行批量插入

@Document(collection="feeds")
public class Feed { 
    @Id
    private String id;
    @Field (value="feed_url")
    private String feedUrl;
    @Field (value="last_read")
    private Date   lastRead;
    private String image;
    private int    status;
    private int    retry;
    ...

当我运行以下代码时,我没有收到任何错误,但是在我的集合中只插入了一个文档。

    ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
    MongoOperations mongoOperations = (MongoOperations) ctx.getBean("mongoTemplate");

    List<Feed> feeds = new LinkedList<Feed>();
    for(int i=0; i<10; i++){
        feeds.add(new Feed("http://myweb.com/"+i));
    }
    mongoOperations.insert(feeds, Feed.class);

如何在一个操作中插入许多文档?

最终我发现了我的问题,我定义了一个唯一索引,因此当我插入第一个文档时,该索引为null,第二个文档也为null。

之后,出现错误,所有其他插入都停止了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM