mongodbTemplate 用法大全
时间: 2024-01-01 14:06:53 浏览: 76
MongoDB是一个流行的文档型数据库,Spring Data MongoDB则是Spring Framework的一个子项目,提供了对MongoDB的集成支持。其中,MongoTemplate是Spring Data MongoDB提供的一个核心类,用于操作MongoDB数据库。
下面是MongoTemplate的用法大全:
1.添加文档
```java
public void insert(Object objectToSave);
public void insert(Object objectToSave, String collectionName);
public void insert(Collection<? extends Object> batchToSave, Class<?> entityClass);
public void insert(Collection<? extends Object> batchToSave, String collectionName);
```
2.修改文档
```java
public void save(Object objectToSave);
public void save(Object objectToSave, String collectionName);
public void save(Collection<? extends Object> objectsToSave, Class<?> entityClass);
public void save(Collection<? extends Object> objectsToSave, String collectionName);
public void updateFirst(Query query, Update update, Class<?> entityClass);
public void updateFirst(Query query, Update update, String collectionName);
public void updateMulti(Query query, Update update, Class<?> entityClass);
public void updateMulti(Query query, Update update, String collectionName);
public void upsert(Query query, Update update, Class<?> entityClass);
public void upsert(Query query, Update update, String collectionName);
```
3.删除文档
```java
public void remove(Object object);
public void remove(Object object, String collectionName);
public void remove(Query query, Class<?> entityClass);
public void remove(Query query, String collectionName);
public void removeAll(Class<?> entityClass);
public void removeAll(String collectionName);
```
4.查询文档
```java
public <T> List<T> findAll(Class<T> entityClass);
public <T> List<T> findAll(String collectionName, Class<T> entityClass);
public <T> T findById(Object id, Class<T> entityClass);
public <T> T findOne(Query query, Class<T> entityClass);
public <T> T findOne(Query query, Class<T> entityClass, String collectionName);
public <T> List<T> find(Query query, Class<T> entityClass);
public <T> List<T> find(Query query, Class<T> entityClass, String collectionName);
public <T> List<T> findDistinct(Query query, String field, Class<?> entityClass, Class<T> resultClass);
public <T> List<T> mapReduce(MapReduceOptions options, Class<?> entityClass, Class<T> resultClass);
public <T> List<T> geoNear(NearQuery near, Class<?> entityClass);
public <T> List<T> geoNear(NearQuery near, Class<?> entityClass, String collectionName);
public <T> List<T> aggregate(TypedAggregation<?> aggregation, Class<T> resultClass);
public <T> List<T> aggregate(Aggregation aggregation, String collectionName, Class<T> resultClass);
public <T> List<T> findAndModify(Query query, Update update, Class<T> entityClass);
public <T> List<T> findAndModify(Query query, Update update, Class<T> entityClass, String collectionName);
public <T> List<T> findAndRemove(Query query, Class<T> entityClass);
public <T> List<T> findAndRemove(Query query, Class<T> entityClass, String collectionName);
```
5.其他操作
```java
public <T> List<T> findDistinct(Query query, String field, Class<?> entityClass, Class<T> resultClass);
public <T> List<T> mapReduce(MapReduceOptions options, Class<?> entityClass, Class<T> resultClass);
public <T> List<T> geoNear(NearQuery near, Class<?> entityClass);
public <T> List<T> geoNear(NearQuery near, Class<?> entityClass, String collectionName);
public <T> List<T> aggregate(TypedAggregation<?> aggregation, Class<T> resultClass);
public <T> List<T> aggregate(Aggregation aggregation, String collectionName, Class<T> resultClass);
public <T> List<T> findAndModify(Query query, Update update, Class<T> entityClass);
public <T> List<T> findAndModify(Query query, Update update, Class<T> entityClass, String collectionName);
public <T> List<T> findAndRemove(Query query, Class<T> entityClass);
public <T> List<T> findAndRemove(Query query, Class<T> entityClass, String collectionName);
```
以上是MongoTemplate的用法大全,涵盖了MongoDB的基本操作。当然,还有其他更高级的用法需要深入学习。
阅读全文