Cloudant 选择器查询
Cloudant Selector Query
我想使用选择器使用 cloudant db 进行查询,例如如下所示:用户希望借入金额超过某个数字的贷款,如何访问 cloudant 选择器中的数组以查找特定记录
{
"_id": "65c5e4c917781f7365f4d814f6e1665f",
"_rev": "2-73615006996721fef9507c2d1dacd184",
"userprofile": {
"name": "tom",
"age": 30,
"employer": "Microsoft"
},
"loansBorrowed": [
{
"loanamount": 5000,
"loandate": "01/01/2001",
"repaymentdate": "01/01/2001",
"rateofinterest": 5.6,
"activeStatus": true,
"penalty": {
"penalty-amount": 500,
"reasonforPenalty": "Exceeded the date by 10 days"
}
},
{
"loanamount": 3000,
"loandate": "01/01/2001",
"repaymentdate": "01/01/2001",
"rateofinterest": 5.6,
"activeStatus": true,
"penalty": {
"penalty-amount": 400,
"reasonforPenalty": "Exceeded the date by 10 days"
}
},
{
"loanamount": 2000,
"loandate": "01/01/2001",
"repaymentdate": "01/01/2001",
"rateofinterest": 5.6,
"activeStatus": true,
"penalty": {
"penalty-amount": 500,
"reasonforPenalty": "Exceeded the date by 10 days"
}
}
]
}
如果您使用默认的 Cloudant 查询索引(键入文本,索引所有内容):
{
"index": {},
"type": "text"
}
那么下面的查询选择器应该可以找到例如所有贷款金额 > 1000 的文件:
"loansBorrowed": { "$elemMatch": { "loanamount": { "$gt": 1000 } } }
我不确定您能否诱使 Cloudant Query 仅索引数组中的嵌套字段,因此,如果您不需要 "index everything" 方法的灵活性,您最好创建一个 Cloudant 搜索索引,它只索引您需要的特定字段。
虽然 Will 的回答有效,但我想让您知道,您可以使用 Cloudant Query 的其他索引选项来处理数组。此博客详细介绍了各种权衡 (https://cloudant.com/blog/mango-json-vs-text-indexes/),但长话短说,我认为这可能是最适合您的索引选项:
{
"index": {
"fields": [
{"name": "loansBorrowed.[].loanamount", "type": "number"}
]
},
"type": "text"
}
与 Will 的索引一切方法不同,这里您仅索引特定字段,如果该字段包含数组,您还索引数组中的每个元素.特别是对于大型数据集上的 "type": "text"
索引,指定要索引的字段将节省您的索引构建时间和存储空间 space。请注意,指定字段的文本索引必须在 "fields":
字段中使用以下形式:{"name": "fieldname", "type": "boolean,number, or string"}
那么相应的 Cloudant 查询 "selector":
语句将是这样的:
{
"selector": {
"loansBorrowed": {"$elemMatch": {"loanamount": {"$gt": 4000}}}
},
"fields": [
"_id",
"userprofile.name",
"loansBorrowed"
]
}
另请注意,您不必将 "fields":
作为 "selector":
语句的一部分,但我在这里只是为了投影 JSON 的某些部分。如果您在 "selector":
语句中省略它,将返回整个文档。
我想使用选择器使用 cloudant db 进行查询,例如如下所示:用户希望借入金额超过某个数字的贷款,如何访问 cloudant 选择器中的数组以查找特定记录
{
"_id": "65c5e4c917781f7365f4d814f6e1665f",
"_rev": "2-73615006996721fef9507c2d1dacd184",
"userprofile": {
"name": "tom",
"age": 30,
"employer": "Microsoft"
},
"loansBorrowed": [
{
"loanamount": 5000,
"loandate": "01/01/2001",
"repaymentdate": "01/01/2001",
"rateofinterest": 5.6,
"activeStatus": true,
"penalty": {
"penalty-amount": 500,
"reasonforPenalty": "Exceeded the date by 10 days"
}
},
{
"loanamount": 3000,
"loandate": "01/01/2001",
"repaymentdate": "01/01/2001",
"rateofinterest": 5.6,
"activeStatus": true,
"penalty": {
"penalty-amount": 400,
"reasonforPenalty": "Exceeded the date by 10 days"
}
},
{
"loanamount": 2000,
"loandate": "01/01/2001",
"repaymentdate": "01/01/2001",
"rateofinterest": 5.6,
"activeStatus": true,
"penalty": {
"penalty-amount": 500,
"reasonforPenalty": "Exceeded the date by 10 days"
}
}
]
}
如果您使用默认的 Cloudant 查询索引(键入文本,索引所有内容):
{
"index": {},
"type": "text"
}
那么下面的查询选择器应该可以找到例如所有贷款金额 > 1000 的文件:
"loansBorrowed": { "$elemMatch": { "loanamount": { "$gt": 1000 } } }
我不确定您能否诱使 Cloudant Query 仅索引数组中的嵌套字段,因此,如果您不需要 "index everything" 方法的灵活性,您最好创建一个 Cloudant 搜索索引,它只索引您需要的特定字段。
虽然 Will 的回答有效,但我想让您知道,您可以使用 Cloudant Query 的其他索引选项来处理数组。此博客详细介绍了各种权衡 (https://cloudant.com/blog/mango-json-vs-text-indexes/),但长话短说,我认为这可能是最适合您的索引选项:
{
"index": {
"fields": [
{"name": "loansBorrowed.[].loanamount", "type": "number"}
]
},
"type": "text"
}
与 Will 的索引一切方法不同,这里您仅索引特定字段,如果该字段包含数组,您还索引数组中的每个元素.特别是对于大型数据集上的 "type": "text"
索引,指定要索引的字段将节省您的索引构建时间和存储空间 space。请注意,指定字段的文本索引必须在 "fields":
字段中使用以下形式:{"name": "fieldname", "type": "boolean,number, or string"}
那么相应的 Cloudant 查询 "selector":
语句将是这样的:
{
"selector": {
"loansBorrowed": {"$elemMatch": {"loanamount": {"$gt": 4000}}}
},
"fields": [
"_id",
"userprofile.name",
"loansBorrowed"
]
}
另请注意,您不必将 "fields":
作为 "selector":
语句的一部分,但我在这里只是为了投影 JSON 的某些部分。如果您在 "selector":
语句中省略它,将返回整个文档。