代码之家  ›  专栏  ›  技术社区  ›  JDT

SQL中嵌套的“for循环”搜索-Azure CosmosDB

  •  1
  • JDT  · 技术社区  · 8 年前

    我正在使用Cosmos DB,并且有一个具有以下简化结构的文档:

    {
        "id1":"123",
        "stuff": [
            {
                "id2": "stuff",
                "a": {
                    "b": {
                        "c": {
                            "d": [
                                {
                                    "e": [
                                        {
                                            "id3": "things",
                                            "name": "animals",
                                            "classes": [
                                                {
                                                    "name": "ostrich",
                                                    "meta": 1
                                                },
                                                {
                                                    "name": "big ostrich",
                                                    "meta": 1
                                                }
                                            ]
                                        },
                                        {
                                            "id3": "default",
                                            "name": "other",
                                            "classes": [
                                                {
                                                    "name": "green trees",
                                                    "meta": 1
                                                },
                                                {
                                                    "name": "trees",
                                                    "score": 1
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
    

    name 看看是否匹配我的搜索词。例如,我想要两者 big trees trees 如果用户键入 .

    For each 文件

    for each stuff

    a.b.c.d[0].e

    对于每个 classes var splice = name.split(' ')

    if (splice.includes(searchWord))

    返回 id1 id2 id3

    使用cosmosDB,我将SQL与以下代码一起使用:

    client.queryDocuments(
        collection,
        `SELECT * FROM root r`
    ).toArray((err, results) => {stuff});
    

    这有效地将我的集合中的每个文档放入一个数组中,以手动执行上面提到的搜索。

    搜索完所有内容后,是否还可以搜索5个最新文档?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Jay Gong    8 年前

    1.有没有人能帮我计算出SQL query能做什么 执行这种功能?

    嗨,JDT。根据您的样品和描述,我建议您使用 ARRAY_CONTAINS

    示例文档:

    [
        {
            "id1": "123",
            "stuff": [
                {
                    "id2": "stuff",
                    "a": {
                        "b": {
                            "c": {
                                "d": [
                                    {
                                        "e": [
                                            {
                                                "id3": "things",
                                                "name": "animals",
                                                "classes": [
                                                    {
                                                        "name": "ostrich",
                                                        "meta": 1
                                                    },
                                                    {
                                                        "name": "big ostrich",
                                                        "meta": 1
                                                    }
                                                ]
                                            },
                                            {
                                                "id3": "default",
                                                "name": "other",
                                                "classes": [
                                                    {
                                                        "name": "green trees",
                                                        "meta": 1
                                                    },
                                                    {
                                                        "name": "trees",
                                                        "score": 1
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        },
        {
            "id1": "456",
            "stuff": [
                {
                    "id2": "stuff2",
                    "a": {
                        "b": {
                            "c": {
                                "d": [
                                    {
                                        "e": [
                                            {
                                                "id3": "things2",
                                                "name": "animals",
                                                "classes": [
                                                    {
                                                        "name": "ostrich",
                                                        "meta": 1
                                                    },
                                                    {
                                                        "name": "trees",
                                                        "meta": 1
                                                    }
                                                ]
                                            },
                                            {
                                                "id3": "default2",
                                                "name": "other",
                                                "classes": [
                                                    {
                                                        "name": "green trees",
                                                        "meta": 1
                                                    },
                                                    {
                                                        "name": "trees",
                                                        "score": 1
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        },
        {
            "id1": "789",
            "stuff": [
                {
                    "id2": "stuff3",
                    "a": {
                        "b": {
                            "c": {
                                "d": [
                                    {
                                        "e": [
                                            {
                                                "id3": "things3",
                                                "name": "animals",
                                                "classes": [
                                                    {
                                                        "name": "ostrich",
                                                        "meta": 1
                                                    },
                                                    {
                                                        "name": "big",
                                                        "meta": 1
                                                    }
                                                ]
                                            },
                                            {
                                                "id3": "default3",
                                                "name": "other",
                                                "classes": [
                                                    {
                                                        "name": "big trees",
                                                        "meta": 1
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    ]
    

    SELECT distinct c.id1,stuff.id2,e.id3 FROM c
    join stuff in c.stuff
    join d in stuff.a.b.c.d
    join e in  d.e
    where ARRAY_CONTAINS(e.classes,{name:"trees"},true)
    or ARRAY_CONTAINS(e.classes,{name:"big trees"},true)
    

    输出:

    enter image description here

    2.搜索完所有内容后,是否也可以搜索最新的5个

    根据我的研究 LIMIT 到目前为止宇宙还不支持。然而, TOP

    select top 5 from c order by c.sort desc
    

    希望对你有帮助。

    推荐文章