質問

I am trying to perform a bulk insert on a list of objects defined with Spring data

@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;
    ...

I get no errors when I run the following code, but only one document is inserted in my collection.

    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);

How can I insert many documents in one operation?

役に立ちましたか?

解決

Finally I found my problem, I had defined a unique index so when I was inserting my first document this index was null and also with the second document.

After it there was an error and all the other inserts where stopted.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top