zoukankan      html  css  js  c++  java
  • mongodb 批量添加、修改和删除

    1.使用MongoTemplate

    a.批量插入

    Insert a Collection of objects into a collection in a single batch write to the database.

    <T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass);

    或Insert a batch of objects into the specified collection in a single batch write to the database.

    <T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName);

    或 Insert a mixed Collection of objects into a database collection determining the collection name to use based on the

     class.(将混合的对象集合插入数据库集合中,根据类确定要使用的集合名称。

    <T> Collection<T> insertAll(Collection<? extends T> objectsToSave);

    b.批量更新

    Updates all objects that are found in the collection for the entity class that matches the query document criteria
    with the provided updated document.

    UpdateResult updateMulti(Query query, Update update, Class<?> entityClass);

    Updates all objects that are found in the specified collection that matches the query document criteria with the
    provided updated document.

    UpdateResult updateMulti(Query query, Update update, String collectionName);

    Updates all objects that are found in the collection for the entity class that matches the query document criteria
    with the provided updated document.

    UpdateResult updateMulti(Query query, Update update, Class<?> entityClass, String collectionName);

    c.批量删除

    Remove all documents that match the provided query document criteria from the the collection used to store the
    entityClass. The Class parameter is also used to help convert the Id of the object if it is present in the query.

    DeleteResult remove(Query query, Class<?> entityClass);

    DeleteResult remove(Query query, Class<?> entityClass, String collectionName);

    DeleteResult remove(Query query, String collectionName);

    d.查找并删除

    <T> List<T> findAllAndRemove(Query query, String collectionName);

    <T> List<T> findAllAndRemove(Query query, Class<T> entityClass);

    <T> List<T> findAllAndRemove(Query query, Class<T> entityClass, String collectionName);

    2.还有其他批量操作的方式。例如基于BulkOperations的操作

    使用形式:

    例:BulkOperations bulkOp = this.template.bulkOps(BulkMode.UNORDERED, COLLECTION_NAME);

    获取BulkOperations对象后,在进行批量操作,具体查看BulkOperations提供的方法。

    最后,execute()执行。

    public Integer batchInsertDetailCommonDailyMessages(List<DetailCommonDailyStatis> messages) {
            if (CollectionUtils.isEmpty(messages)) {
                return 0;
            }
            // 插入新数据
            BulkOperations bulkOp = this.template.bulkOps(BulkMode.UNORDERED, COLLECTION_NAME); //BulkNode有两种形式
            bulkOp.insert(messages);
            BulkWriteResult result = bulkOp.execute();int insertCount = result.getInsertedCount();return insertCount;
        }

    3.基于MongoRepository

  • 相关阅读:
    Sqlserver根据条件去除重复数据并且留下的是最大值数据
    C# Linq及Lamda表达式实战应用之 GroupBy 分组统计
    MVVM模式WPF的ComboBox数据绑定,使用Dictionary作为数据源
    C# System.Timers.Timer定时器的使用和定时自动清理内存应用
    SQL优化策略
    只要不放弃,总有出头之路
    2 Python基础
    4 动态库和静态库
    1 VS常用快捷键
    2 C语言环境、编译
  • 原文地址:https://www.cnblogs.com/muxi0407/p/11765140.html
Copyright © 2011-2022 走看看