# @care-portals/node-json-criteria 中文文档

规范地址：https://npmcn.com/package/%40care-portals/node-json-criteria/

版本：3\.0\.1
npm 元数据更新时间：2025-12-12T16:46:18.147Z
中文译文更新时间：2026-11-09T17:15:03.395Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40care-portals/node-json-criteria
原文对照：https://npmcn.com/package/%40care-portals/node-json-criteria/?lang=original
项目主页：https://github.com/care-portals/node-json-criteria
源码仓库：https://github.com/care-portals/node-json-criteria

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：177 kB，107 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>=18\.0\.0
- 运行依赖：1 个
- 首次发布：2025-11-25T20:45:41.845Z
- 最新版本发布：2025-12-12T16:46:17.940Z
- 近 30 天下载量：2159（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 2 个）

## 中文简介

评价 JSON 对象 MongoDB 样式的标准查询 .

## 安装

```sh
npm install @care-portals/node-json-criteria
```

也可以使用：`pnpm add @care-portals/node-json-criteria`、`yarn add @care-portals/node-json-criteria`、`bun add @care-portals/node-json-criteria`

## README

## 内容提要 [![Build Status](https://travis-ci.org/mirek/node-json-criteria.png?branch=master)](https://travis-ci.org/mirek/node-json-criteria)

JSON 对象 Mongo 样式的标准查询 .

## 安装

```
npm install @care-portals/node-json-criteria --save
```

## 使用量

### 常见JS

```javascript
const { test } = require("@care-portals/node-json-criteria");
console.log(test({ foo: 1 }, { foo: { $gt: 0 } }));
```

### ES 模块

```javascript
import { test } from "@care-portals/node-json-criteria";
console.log(test({ foo: 1 }, { foo: { $gt: 0 } }));
```

## 行动

标准查询遵循蒙哥发行公约。 您可以使用描述于 [http://docs.mongodb.org/manual/参考文献/操作者/query](http://docs.mongodb.org/manual/reference/operator/query)

-   逻辑操作
    -   `{ $and: [ ... ] }` - 全部来
    -   `{ $or: [ ... ] }` - 任何一个
    -   `{ $nor: [ ... ] }` - 没有
    -   `{ $not: ... }` - 才没有呢. `{ $not: { $gt: 0, $lt: 1 } }`
-   比较操作
    -   `{ field: ... }` - 平等(隐性)
    -   `{ field: { $eq: ... } }` - 平等(明确)
    -   `{ field: { $ne: ... } }` - 不对
    -   `{ field: { $gt: ... } }` - 超过
    -   `{ field: { $gte: ... } }` - 大于或等于
    -   `{ field: { $lt: ... } }` - 低于
    -   `{ field: { $lte: ... } }` - 低于或等于
    -   `{ field: { $in: [ ... ] } }` - 至少一个元素匹配值(或如果数组值的元素)
    -   `{ field: { $nin: [ ... ] } }` - 没有元素匹配值(或值的元素如果数组)
-   元素行动
    -   `{ field: { $exists: true/false } }` - 字段存在
    -   `{ field: { $type: 'number|string|...' } }` - 匹配字段类型
-   评价业务
    -   `{ field: { $mod: [ div, rem ] } }` - 除以div有提醒雷姆
    -   `{ field: { $regexp: '...', $options: 'i' } }` - 将正则表达式与可选选项匹配
    -   `{ field: { $where: function (v) { return true/false } } }` - 使用提供的功能进行测试,用于安全目的,因为不支持字符串
-   阵列操作
    -   `{ field: { $all: [ ... ] } }` - 所有值都在字段值中
    -   `{ field: { $elemMatch: ... } }` - 至少一个元素匹配
    -   `{ field: { $size: ... } }` - 匹配字段数组值的长度
    -   `{ field: { $first: ... } }` - 匹配数组的第一个元素
    -   `{ field: { $last: ... } }` - 匹配数组的最后一个元素
    -   `{ field: { $pop: ... } }` - 匹配数组的最后一个元素(类似于 Array.pop ())

更多例子请查看图示.

# 示例使用大小写

假设你有基于JSON的 RESTFOR API,你想用摩卡测试:

```
// spec/spec-my-api.js

var assert = require('assert')
var request = require('request')
var jc = require('@care-portals/node-json-criteria')
var endpoint = 'http://localhost:3000/api/v1'

function get (path, criteria) {
  return function (done) {
    request
      .get(endpoint + path)
      .end(function (err, resp) {
        assert.ifError(err)
        assert.ok(resp.body.count > 0)
        assert.ok(jc.test(resp.body, criteria))
        done(err)
      })
  }
}

describe('api', function () {

  it('should have zero users', get('users/count', { count: 0 }))

  it('should have empty user list', get('users', { users: { $size: 0 } }))

  it('should have 3 to 5 products', get('products', { 'meta.count': { $gt: 2, $lte: 5 } }))

  it('should have 3 products with meta count check', get('products', {
    $and: [
      { 'meta.count': 3 },
      { products: { $size: 3 } }
    ]
  }))

  it('should have shipped flag', get('orders/1', {
    tags: { $in: [ 'shipped' ] }
  }))

})
```

□ 许可证

```
The MIT License (MIT)

Copyright (c) 2014 Mirek Rusin http://github.com/mirek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

## 常见问题

### @care-portals/node-json-criteria 是什么？

评价 JSON 对象 MongoDB 样式的标准查询 \.

### 如何安装 @care-portals/node-json-criteria？

在项目目录运行 npm install @care-portals/node-json-criteria，或使用 pnpm add / yarn add / bun add。

### @care-portals/node-json-criteria 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/care-portals\_\_node-json-criteria 是否存在。

### @care-portals/node-json-criteria 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @care-portals/node-json-criteria 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=18\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @care-portals/node-json-criteria 可以商用吗？

@care-portals/node-json-criteria 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @care-portals/node-json-criteria 的下载量有多少？

根据 npm 官方下载统计，@care-portals/node-json-criteria 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,159 次，日均约 72 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @care-portals/node-json-criteria 还在维护吗？

最新版本 3\.0\.1 发布于 2025/12/13（10 个月前）；至今共发布 2 个版本，其中过去 12 个月发布了 2 个；首次发布于 2025/11/26。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

