# 模型（models）

> 原文：[Models](http://mongoosejs.com/docs/models.htm)\
> 翻译：小虾米（QQ:509129）

## Models

从我们的Schema定义[模型](http://mongoosejs.com/docs/api.html#model-js)的构造函数编译。实例这些模型代表[文档](http://mongoosejs.com/docs/documents.html)可以从我们的数据库中保存和检索。从数据库中依靠这些模型来操作所有文档创建和检索。

### 编译你的第一个模型

```javascript
var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var Tank = mongoose.model('Tank', schema);
```

第一个参数是你的模型集合的单数名称。Mongoose会自动寻找你的模型名称的复数形式。因此，对于上面的示例，模型`Tank`是用于数据库中的`tanks`集合的。.model() 功能使得到schema的副本。确信你已经添加了你想要的一切在调用.model()之前！

### 构建文档

文档是我们模型的实例。创建它们，并保存到数据库是很容易的：

```javascript
var Tank = mongoose.model('Tank', yourSchema);

var small = new Tank({ size: 'small' });
small.save(function (err) {
  if (err) return handleError(err);
  // saved!
})

// or

Tank.create({ size: 'small' }, function (err, small) {
  if (err) return handleError(err);
  // saved!
})
```

请注意，没有`tanks` 将创建/删除，直到连接您的模型使用是打开的。每一个模型都有一个关联的连接。当你使用`mongoose.model()`。你的模型将要使用默认mongoose连接。

```javascript
mongoose.connect('localhost', 'gettingstarted');
```

如果你创建一个自定义的连接，使用连接的`model()`函数代替。

```javascript
var connection = mongoose.createConnection('mongodb://localhost:27017/test');
var Tank = connection.model('Tank', yourSchema);
```

### 查询

对于Mongoosecha的查找文档很容易，它支持[丰富](http://www.mongodb.org/display/DOCS/Advanced+Queries)的查询MongoDB语法。文件可以使用每个模型中使用 [find](http://mongoosejs.com/docs/api.html#model_Model.find)，[findById](http://mongoosejs.com/docs/api.html#model_Model.findById)，[findOne](http://mongoosejs.com/docs/api.html#model_Model.findOne)，或者[where](http://mongoosejs.com/docs/api.html#model_Model.where)，静态方法。

```javascript
Tank.find({ size: 'small' }).where('createdDate').gt(oneYearAgo).exec(callback);
```

查看关于如何使用[查询](http://mongoosejs.com/docs/api.html#query-js)API的详细信息的[querying](http://mongoosejs.com/docs/queries.html)章节。

### 删除

模型有一个静态删除方法，可用于移除所有匹配条件的文档。

```javascript
Tank.remove({ size: 'large' }, function (err) {
  if (err) return handleError(err);
  // removed!
});
```

### 更新

每个模型都有自己的更新方法，用于修改数据库中的文档，不将它们返回到您的应用程序。

详细看[API](http://mongoosejs.com/docs/api.html#model_Model.update)文档。

如果你想要更新一个文档数据库，并将结果返回给你的应用程序，使用[`findOneAndUpdate`](http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate)代替。

### 然而，更多的

API文档包含了许多额外的方法，像 [count](http://mongoosejs.com/docs/api.html#model_Model.count)，[mapReduce](http://mongoosejs.com/docs/api.html#model_Model.mapReduce)，[aggregate](http://mongoosejs.com/docs/api.html#model_Model.aggregate)，[更多](http://mongoosejs.com/docs/api.html#model_Model.findOneAndRemove)。


---

# 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://mongoose.shujuwajue.com/guide/models.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.
