如何在 Alasql 中检查对象是否为空

How to check if object is empty in Alasql

提问人:Matan Gubkin 提问时间:4/18/2022 更新时间:9/7/2022 访问量:221

问:

我这里有这个例子,我试图只查询空的mongodb数组


var data = [{
    "dynamic_env": null,
    "mission_critical": null,
    "chart-version": null,
    "app-url": null,
    "mongodb": {},
    "nightly_shutdown": false,
    "external_values": null,
    "external_values_source": null,
    "external_values_path": null,
    "atlas_database": null,
    "allow_db_restore": null,
    "on-demand": false,
    "helm_timeout": null,
    "aws-account": null,
    "post": [],
    "pre": [],
    "atlas_project": null,
    "istio-enabled": false,
    "env_subscription": null,
    "istio-auth-policy": null,
    "version": null,
    "e2e-tests-script": [],
    "morning_startup": true,
    "project_dst": null,
    "cred": null,
    "dataset": null,
    "project_src": null,
    "Env": {
        "identifier": "cm-infra-ci-core1@admin365-infra-stg-eks@admin365-cm-infra-ci-core1",
        "title": "cm-infra-ci-core1"
    },
    "namespace": "admin365-cm-infra-ci-core1",
    "cluster": {
        "identifier": "admin365-infra-stg-eks@infra-stg",
        "title": "admin365-infra-stg-eks"
    },
    "Service": {
        "identifier": "user-data-management-gdpr-fetch",
        "title": "user-data-management-gdpr-fetch"
    },
    "team": null,
    "$identifier": "user-data-management-gdpr-fetch@cm-infra-ci-core1@admin365-cm-infra-ci-core1",
    "$title": "user-data-management-gdpr-fetch@cm-infra-ci-core1"
},
{
    "dynamic_env": null,
    "mission_critical": null,
    "chart-version": null,
    "app-url": null,
    "mongodb": {a: 123},
    "nightly_shutdown": false,
    "external_values": null,
    "external_values_source": null,
    "external_values_path": null,
    "atlas_database": null,
    "allow_db_restore": null,
    "on-demand": false,
    "helm_timeout": null,
    "aws-account": null,
    "post": [],
    "pre": [],
    "atlas_project": null,
    "istio-enabled": false,
    "env_subscription": null,
    "istio-auth-policy": null,
    "version": null,
    "e2e-tests-script": [],
    "morning_startup": true,
    "project_dst": null,
    "cred": null,
    "dataset": null,
    "project_src": null,
    "Env": {
        "identifier": "cm-infra-ci-core1@admin365-infra-stg-eks@admin365-cm-infra-ci-core1",
        "title": "cm-infra-ci-core1"
    },
    "namespace": "admin365-cm-infra-ci-core1",
    "cluster": {
        "identifier": "admin365-infra-stg-eks@infra-stg",
        "title": "admin365-infra-stg-eks"
    },
    "Service": {
        "identifier": "user-data-management-gdpr-fetch",
        "title": "user-data-management-gdpr-fetch"
    },
    "team": null,
    "$identifier": "user-data-management-gdpr-fetch@cm-infra-ci-core1@admin365-cm-infra-ci-core1",
    "$title": "user-data-management-gdpr-fetch@cm-infra-ci-core1"
}];

var res = alasql(`SELECT * FROM ? WHERE ('mongodb'->() = {})`,[data]);    

console.log(res.length); // [{"a":1,"b":40},{"a":2,"b":20}]
alasql

评论


答:

0赞 dataman 9/7/2022 #1

添加一个用户定义的函数,该函数返回对象中的键数,然后对其进行查询:

alasql.fn.keylen = function(x) { return Object.keys(x).length; }
var res = alasql(`SELECT * FROM ?  WHERE keylen(mongodb) = 0`,[data]);