如何编写 N1QL 查询以获取所有 ordrqty 等于 102 的 SaleDt1

How to write a N1QL query to get all the SaleDt1 which has ordrqty equal to 102

/* 下面是示例文档*/

 [
     {
    "trans-data": 
        { 
       "$Docver":"1.0",
        "ManufId":"1234543",
        "ToyDot": 
                 {
                   "GrossAmt":"675",
                   "Wqty":"200"
                  },
       "Mflnitmlst": 
                   [
                      {
                        "Mfprcdv":"25000",
                        "SaleDt1":
                          [
                            {
                              "Mtid":"0987655",
                              "ordrqty":"102",
                              "Plainqty":"1000"
                             }
                          ]
                       },
                      {
                        "Mfprcdv":"25000",
                        "SaleDt1":
                          [
                            {
                              "Mtid":"0987656",
                              "ordrqty":"110",
                              "Plainqty":"1500"
                             }
                          ]
                      },
                     {
                        "Mfprcdv":"25000",
                        "SaleDt1":
                          [
                            {
                              "Mtid":"0987657",
                              "ordrqty":"120",
                              "Plainqty":"2000"
                             }
                          ]
                      }
                   ],
        "Tmstp":"2021-03-04T14:23:21",
        "Sectn":"XYZ"
       }
     },
    {
    "trans-data": 
        {
        "$Docver":"1.0",
        "ManufId":"1234543",
        "ToyDot": 
                 {
                   "GrossAmt":"605",
                   "Wqty":"100"
                  },
        "Mflnitmlst": 
                   [
                      {
                        "Mfprcdv":"26000",
                        "SaleDt1":
                          [
                            {
                              "Mtid":"1987655",
                              "ordrqty":"102",
                              "Plainqty":"1000"
                             }
                          ]
                       },
                      {
                        "Mfprcdv":"26000",
                        "SaleDt1":
                          [
                            {
                              "Mtid":"1987656",
                              "ordrqty":"110",
                              "Plainqty":"1500"
                             }
                          ]
                      },
                     {
                        "Mfprcdv":"26000",
                        "SaleDt1":
                          [
                            {
                              "Mtid":"1987657",
                              "ordrqty":"120",
                              "Plainqty":"2000"
                             }
                          ]
                      }
                   ],
        "Tmstp":"2021-03-04T14:23:21",
        "Sectn":"XYZ"
       }
     }
    ]

                 

假设您的文档以 ARRAY

开头
SELECT dmfl.SaleDt1
FROM mybucket AS mb
UNNEST mb AS d
UNNEST d.`trans-data`.Mflnitmlst AS dmfl
WHERE ANY v IN dmfl.SaleDt1 SATISFIES v.ordrqty = "102" END;

如果你想要只有“102”的对象

SELECT sld.*
FROM mybucket AS mb
UNNEST mb AS d
UNNEST d.`trans-data`.Mflnitmlst AS dmfl
UNNEST dmfl.SaleDt1 AS sld
WHERE sld.ordrqty = "102";

如果您的文档不是 ARRAY,它就是对象

SELECT sld.*
FROM mybucket AS mb
UNNEST mb.`trans-data`.Mflnitmlst AS dmfl
UNNEST dmfl.SaleDt1 AS sld
WHERE sld.ordrqty = "102";

结帐https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/indexing-arrays.html

更新:使用 INSERT AND SELECT

INSERT INTO default VALUES ("f01", [ { "trans-data": { "$Docver":"1.0", "ManufId":"1234543", "ToyDot": { "GrossAmt":"675", "Wqty":"200" }, "Mflnitmlst": [ { "Mfprcdv":"25000", "SaleDt1": [ { "Mtid":"0987655", "ordrqty":"102", "Plainqty":"1000" } ] }, { "Mfprcdv":"25000", "SaleDt1": [ { "Mtid":"0987656", "ordrqty":"110", "Plainqty":"1500" } ] }, { "Mfprcdv":"25000", "SaleDt1": [ { "Mtid":"0987657", "ordrqty":"120", "Plainqty":"2000" } ] } ], "Tmstp":"2021-03-04T14:23:21", "Sectn":"XYZ" } }, { "trans-data": { "$Docver":"1.0", "ManufId":"1234543", "ToyDot": { "GrossAmt":"605", "Wqty":"100" }, "Mflnitmlst": [ { "Mfprcdv":"26000", "SaleDt1": [ { "Mtid":"1987655", "ordrqty":"102", "Plainqty":"1000" } ] }, { "Mfprcdv":"26000", "SaleDt1": [ { "Mtid":"1987656", "ordrqty":"110", "Plainqty":"1500" } ] }, { "Mfprcdv":"26000", "SaleDt1": [ { "Mtid":"1987657", "ordrqty":"120", "Plainqty":"2000" } ] } ], "Tmstp":"2021-03-04T14:23:21", "Sectn":"XYZ" } } ]);

SELECT dmfl.SaleDt1
FROM default AS mb USE KEYS "f01"
UNNEST mb AS d
UNNEST d.`trans-data`.Mflnitmlst AS dmfl
WHERE ANY v IN dmfl.SaleDt1 SATISFIES v.ordrqty = "102" END;

如果存储桶名称是 trans-data 并且您的示例文档来自 select 查询

SELECT dmfl.SaleDt1
FROM `trans-data` AS mb
UNNEST mb.Mflnitmlst AS dmfl
WHERE ANY v IN dmfl.SaleDt1 SATISFIES v.ordrqty = "102" END;