# 12.1.3　使用 MongoTemplate 访问 MongoDB

我们已经在配置类中配置了 MongoTemplate bean，不管是显式声明还是扩展 AbstractMongoConfiguration 都能实现相同的效果。接下来，需要做的就是将其注入到使用它的地方：

```java
@Autowired
MongoOperations mongo;
```

注意，在这里我们将 MongoTemplate 注入到一个类型为 MongoOperations 的属性中。MongoOperations 是 MongoTemplate 所实现的接口，不使用具体实现是一个好的做法，尤其是在注入的时候。

MongoOperations 暴露了多个使用 MongoDB 文档数据库的方法。在这里，我们不可能讨论所有的方法，但是可以看一下最为常用的几个操作，比如计算文档集合中有多少条文档。使用注入的 MongoOperations，我们可以得到 Order 集合并调用 count() 来得到数量：

```java
long orderCount = mongo.getCollection("order").count();
```

现在，假设要保存一个新的 Order。为了完成这个任务，我们可以调用 save() 方法：

```java
Order order = new Order();
... // set properties and add line item
mongo.save(order, "order");
```

save() 方法的第一个参数是新创建的 Order，第二个参数是要保存的文档存储的名称。

另外，我们还可以调用 findById() 方法来根据 ID 查找订单：

```java
String orderId = ...;
Order order = mongo.findById(orderId, Order.class);
```

对于更高级的查询，我们需要构造 Query 对象并将其传递给 find() 方法。例如，要查找所有 client 域等于 “Chuck Wagon” 的订单，可以使用如下的代码：

```java
List<Order> chucksOrders = mongo.find(
  Query.query(Criteria.where("client").is("Chuck Wagon")), Order.class);
```

在本例中，用来构造 Query 对象的 Criteria 只检查了一个域，但是它也可以用来构造更加有意思的查询。比如，我们想要查询 Chuck 所有通过 Web 创建的订单：

```java
List<Order> chucksWebOrders = mongo.find(Query.query(
  Criteria.where("customer").is("Chuck Wagon")
    .and("type").is("WEB")), Order.class);
```

如果你想移除某一个文档的话，那么就应该使用 remove() 方法：

```java
mongo.remove(order);
```

如我前面所述，MongoOperations 有多个操作文档数据的方法。我建议你查看一下其 JavaDoc 文档，以了解通过 MongoOperations 都能完成什么功能。

通常来讲，我们会将 MongoOperations 注入到自己设计的 Repository 类中，并使用它的操作来实现 Repository 方法。但是，如果你不愿意编写 Repository 的话，那么 Spring Data MongoDB 能够自动在运行时生成 Repository 实现。下面，我们来看一下是如何实现的。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://potoyang.gitbook.io/spring-in-action-v4/untitled-6/untitled-3/untitled-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
