Example usage for org.springframework.data.mongodb.core MongoActionOperation INSERT_LIST

List of usage examples for org.springframework.data.mongodb.core MongoActionOperation INSERT_LIST

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core MongoActionOperation INSERT_LIST.

Prototype

MongoActionOperation INSERT_LIST

To view the source code for org.springframework.data.mongodb.core MongoActionOperation INSERT_LIST.

Click Source Link

Usage

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

protected List<ObjectId> insertDBObjectList(final String collectionName, final List<DBObject> dbDocList) {
    if (dbDocList.isEmpty()) {
        return Collections.emptyList();
    }//from  www  .j a v  a 2s.c o m

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("insert list of DBObjects containing " + dbDocList.size() + " items");
    }
    execute(collectionName, new CollectionCallback<Void>() {
        public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST,
                    collectionName, null, null, null);
            WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
            WriteResult wr;
            if (writeConcernToUse == null) {
                wr = collection.insert(dbDocList);
            } else {
                wr = collection.insert(dbDocList.toArray((DBObject[]) new BasicDBObject[dbDocList.size()]),
                        writeConcernToUse);
            }
            handleAnyWriteResultErrors(wr, null, "insert_list");
            return null;
        }
    });

    List<ObjectId> ids = new ArrayList<ObjectId>();
    for (DBObject dbo : dbDocList) {
        Object id = dbo.get(ID);
        if (id instanceof ObjectId) {
            ids.add((ObjectId) id);
        } else {
            // no id was generated
            ids.add(null);
        }
    }
    return ids;
}