# mongo-adapt

> 对于 mongodb 进行简单的封装，便于使用

Latest version **0.0.11** (published 2018-12-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongo-adapt
pnpm add mongo-adapt
yarn add mongo-adapt
bun add mongo-adapt
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.11 |
| Published | 2018-12-11 |
| First published | 2018-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 26 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Tenny |
| Maintainers | fly_dream |
| Keywords | nodejs, mongodb, mongo-adapt |

## Links

- npm: https://www.npmjs.com/package/mongo-adapt
- Repository: https://gitee.com/towardly/mongo-adapt
- Issues: https://gitee.com/towardly/mongo-adapt/issues
- npm.io page: https://npm.io/package/mongo-adapt

## Dependencies (1)

- [mongodb](https://npm.io/package/mongodb.md) ^3.1.1

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.0.11 (latest) — 2018-12-11
- 0.0.10 — 2018-12-05
- 0.0.9 — 2018-12-05
- 0.0.8 — 2018-11-21
- 0.0.7 — 2018-11-20
- 0.0.6 — 2018-11-19
- 0.0.5 — 2018-11-15
- 0.0.4 — 2018-11-12
- 0.0.3 — 2018-11-12
- 0.0.2 — 2018-09-07
- 0.0.1 — 2018-09-07

## README

## mongo-adapt
对于 `mongodb` 的使用进行一个简单的封装。轻量化，没有做格式化验证。后续会不段的添加新的函数。
### 安装
```javascript
npm install mongo-adapt
```
### 使用
#### 连接 mongo
```javascript
const MongoAdapt = require('mongo-adapt');
const mongoAdapt = new MongoAdapt(mongoConfig, indexes);
```
`mongoConfig` -- 连接到 mongo 的配置地址，必须是 `Object` 类型的。如下格式：
```javascript
{
  "name1": "mongodb://192.168.1.70:27017/test"
}
```
如果是这样的, 则 `key` 为**连接名**，为每一个数据库连接取一个连接名，方便后续使用；`value` 为数据库连接地址，最好将常用的数据库放到连接地址上，这样就不需要在每一次使用的时候，都传递使用的数据库。

如果需要连接多个地址，则在配置里面继续添加。最先配置的作为默认使用的连接。

如果不想直接将数据库拼到连接地址上，也可以通过配置的形式进行分开。
```javascript
{
  "name1": {
    "url": "mongodb://192.168.1.70:27017", // 数据库连接地址
    "db": "test" // 默认使用的数据库名称
  }
}
```
后续的对于数据库的操作包括增删改查(CRUD)，则使用上面创建的 `mongoAdapt`；例如：
```javascript
mongoAdapt.conn()
mongoAdapt.close()
mongoAdapt.find()
mongoAdapt.update()
mongoAdapt.delete()
mongoAdapt.aggregate()
```
`indexes` -- 在连接数据库成功后, 创建索引。如下格式：
```javascript
{
  // name1 必须跟配置数据库连接时的 key 保持一致
  name1: [{
    db: 'workstation', // 数据库名称
    name: 'users', // 表名(collection)
    indexes: [{
      key: { tel: 1 },
      unique: true,
      background: true
    }] // 所有的索引 -- createIndexes()
  }]
}
```
关于里面的 `indexes` 字段, 详细参考：[createIndexes](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#createIndexes "createIndexes")的`indexSpecs`; [mongo-createIndexes](https://docs.mongodb.com/manual/reference/method/db.collection.createIndexes/index.html "mongo-createIndexes")是官方的明细。
#### 接口说明
`conn(mongoConfig)`

&emsp;进行数据库连接，默认的时候不需要调用该函数，因为在创建 `MongoAdapt` 已经调用过了；但是也可以自己手动调用该函数重新进行数据库连接。

`close(clientName)`

&emsp;关闭某个数据库连接，`clientName` 就是进行数据库连接时的**连接名**。如果某个数据库连接只使用一次，可以使用该函数进行断开。一旦断开后，对于数据库的CRUD操作则不被允许。

`closeAll()`

&emsp;断开所有的数据库连接。

`db({})`

&emsp;获取数据库对象, 对应于 [DB](http://mongodb.github.io/node-mongodb-native/3.1/api/Db.html "Db")，参数如下：
* **client**: `String`，数据库**连接名**
* **db**：`String`，数据库名称

`collection(connname, dbConfig)`

&emsp;获取数据集，对应于 [Collection](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html "Collection")，参数如下：
* **connname**: `String`，数据集的名称
* **dbConfig**：`Object`，调用 `db({})` 函数的参数一致，包括 `client` 和 `db`

`insert(connname, docs, insertOptions, options)`

&emsp;往数据集中插入文档
* **connname**：`String`，数据集的名称(connection name)
* **docs**：`Array|Object`，如果类型为 `Array`，则往数据集中插入多条数据，调用原生的 `insertMany`；否则插入一条文件，调用 `insertOne`
* **insertOptions**：调用 `insertMany`或`insertOne`时的第二个参数；详情参考：[insertMany](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#insertMany "insertMany")、[insertOne](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#insertOne "insertOne")
* **options**: `Object{client, db}`，用于进行切换数据库时调用，如果使用默认的连接和数据库，则可以不传递该参数。

`find(connname, query, findOptions, options)`

&emsp;查询多条文档，参考：[find](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#find "find")，其中 `query` 和 `findOptions` 参数即为调用原生的 `find(query, options)` 时需要的参数
* **connname**：`String`，数据集的名称(connection name)
* **query**: `Object`，查询条件
* **findOptions**: `Object`，查询的配置，这里面除了和原生的`find`的`options`保持一致外，对了一个配置 `page` 表明查询第几页的数据，默认为 1，做分页查询时使用，这个时候，同时需要配置 `limit` 默认为 10
* **options**: `Object{client, db}`，用于进行切换数据库时调用，如果使用默认的连接和数据库，则可以不传递该参数。

`findOne(connname, query, findOptions, options)`

&emsp;查询第一条匹配的文档，[findOne](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#findOne "findOne")

`findById(connname, id, projection, options)`

&emsp;根据 id 查询文档，如果传递的参数 options.raw = true 则不会将 id 转换为 ObjectID 类型进行查找；否则在查询的时候会将 id 转换为 ObjectID 类型作为查询条件
* **options**：`Object{client, db, raw}`
  * **raw**：`Boolean`，递的 id 是否需要转换为 ObjectId 类型，true 为不转换，false 为转换(默认)

`count(connname, query, countOptions, options)`

&emsp;统计符合体条件的文档数量，参考：[countDocuments](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments "countDocuments")

`update(connname, filter, update, updateOptions, options)`

&emsp;更新文档，注意 `update` 参数需要 `operator`，参考：[updateMany](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#updateMany "updateMany")、[updateOne](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#updateOne "updateOne")
* **options**：`Object{client, db, multi: true}`
  * **multi**：`Boolean` 是否更新多条文档，默认为true；区别是调用 `updateMany` 还是 `updateOne`

`upset(connname, filter, doc, options|upsert)`

&emsp;更新文档，使用 `$set` 操作
* **doc**：`Object` 更新文档字段内容
* **options|upsert**：`options - Object | upsert - Boolean`；如果值的类型为 `Boolean` 类型的，则表示传递的是 `upsert` 参数，使用的数据库连接为默认的，并且表示更新多条符合条件的记录；同时也支持对象形式参数 `Object{ client, db, multi: true, upsert }`

`upsetById(connname, _id, doc, options|upsert)`

&emsp; 根据 `_id` 修改文档, 使用 `$set` 操作符
* **_id**：`String`，不需要转换为 `ObjectId` 类型，会自动进行转换
* **options**：`options - Object | upsert - Boolean`；如果值的类型为 `Boolean` 类型的，则表示传递的是 `upsert` 参数，使用的数据库连接为默认的，并且 `_id` 将会转换为 `ObjectId` 类型；也支持参数为对象 `Object{ client, db, raw, upsert }`

`delete(connname, filter, deleteOptions, options)`

&emsp;删除文档，详细：[deleteMany](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#deleteMany "delete")、[deleteOne](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#deleteOne "deleteOne")

`deleteById(connname, _id, options)`

&emsp; 根据 `_id` 删除文档
* **options**: `Object{ client, db, raw }`

`createIndex(connname, fieldOrSpec, indexOption, options)`

&emsp;创建索引, 详情参见文档：[createIndex](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#createIndex "createIndex")

`createIndexes(connname, indexSpecs, options)`

&emsp;创建多条索引, 详细：[createIndexes](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#createIndexes "createIndexes")

`aggregate(connname, pipeline, options)`

&emsp;执行管道操作，[aggregate](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#aggregate "aggregate")

---
_Source: https://npm.io/package/mongo-adapt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
