多条件模糊查询
查询条件:
需要在Hosts文档中同时模糊查询字段hostIp与字段localIp并且组合条件查询出categoryTag字段与hostType字段,categoryTag字段与hostType字段查询未非模糊查询(上图状态条件先不作为条件查询)
const where = {
$or:[{ hostIp: new RegExp(47) }, { localIp: new RegExp(47) }],
categoryTag: '主机A',
hostType: '1',
}
await this.ctx.model.Hosts.find(where)
通过postman测试接口可得:
{
"success": true,
"payload": {
"list": [
{
"customHostGroups": [],
"_id": "5d2dd2a4c5b5c21ae00fcfec",
"tenantId": "2ba56b7c3f8c49598207d2a9fe54970b",
"hostIp": "22.106.182.777",
"localIp": "47.18.231.33",
"sshPort": 22,
"sshUsername": "root",
"sshConnectionType": 0,
"sshPassword": "abcd123123",
"hostType": 1,
"accessWay": 0,
"isAllocated": false,
"isAllocatedToEnv": false,
"projectId": null,
"projectName": null,
"envId": null,
"envName": null,
"dr": 0,
"_created_at": "2019-07-16T13:35:32.308Z",
"_updated_at": "2019-07-16T13:35:32.308Z",
"categoryTag": "主机A"
}
],
"page": 2,
"size": 1,
"total": 1,
"totalPage": 1
}
}
模糊查询主要是mongoose的RegExp的使用,配合$or则为多条件模糊查询
$or 用于多条件查询 http://www.nodeclass.com/api/mongoose.html#query_Query-or
$regex 用于模糊查询 http://www.nodeclass.com/api/mongoose.html#query_Query-regex