2

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.