索引和查询数组,使用cloudant和couchdb 2.0的芒果查询 [英] index and query items in an array with mango query for cloudant and couchdb 2.0
问题描述
我具有以下数据库结构:
I have the following db structure:
{"_id": "0096874","genre": ["Adventure","Comedy", "Sci-Fi" ]}
{"_id": "0099088","genre": ["Comedy", "Sci-Fi", "Western"]}
并希望像在mongodb中一样进行查询
and like to query it like I could do in mongodb
db.movies.find({genre: {$in: ["Comedy"]}})
当我在整个数据库中使用文本索引时,它可以工作,但这似乎非常浪费:
It works when i use a text index for the whole database, but that seems very wasteful:
// index
{
"index": {},
"type": "text"
}
//query
{
"selector": {
"genre": {
"$in": ["Comedy"]
}
},
"fields": [
"_id",
"genre"
]
}
以下索引不起作用:
{
"index": {
"fields": [
"genre"
]
},
"type": "json"
}
什么是cloudant查询的正确索引,而不是整个数据库的索引? 谢谢您的帮助
What is the correct index for cloudant query, which does not index the whole db? Thanks for your help
推荐答案
您几乎完全正确.您的索引正确,但是您需要使用选择器来获取所有ID https ://cloudant.com/blog/mango-json-vs-text-indexes/.
You had it almost correct. Your index is right, but you need to throw in a selector to get all IDs https://cloudant.com/blog/mango-json-vs-text-indexes/.
正如Tony所说,这不是解决方案性能的好方法,
This isn't a great solution performance-wise, as Tony says,
之所以可行,是因为,Mango对所有文档执行上述$ in操作作为过滤机制.正如我们在上一节有关JSON语法的结论中所看到的,上述查询的性能折衷在于,它本质上执行完整索引扫描,然后应用过滤器.
{
"selector": {
"_id": {
"$gt": null
},
"genre": {
"$in": ["Western"]
}
},
"fields": [
"_id",
"genre"
],
"sort": [
{
"_id": "asc"
}
]
}
这篇关于索引和查询数组,使用cloudant和couchdb 2.0的芒果查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!