Open uniquejava opened 6 years ago
Operator | Argument | Purpose |
---|---|---|
$all |
Array | Matches an array value if it contains all the elements of the argument array. |
$allMatch |
Selector | Matches and returns all documents that contain an array field, where all the elements match all the specified query criteria. |
$and |
Array | Matches if all the selectors in the array match. |
$elemMatch |
Selector | Matches and returns all documents that contain an array field with at least one element that matches all the specified query criteria. |
$nor |
Array | Matches if none of the selectors in the array match. |
$not |
Selector | Matches if the selector does not match. |
$or |
Array | Matches if any of the selectors in the array match. All selectors must use the same index. |
所有语言的crud操作: https://github.com/cloudant/haengematte
相关的文档: https://console.bluemix.net/docs/services/Cloudant/libraries/supported.html
比如crud with node: https://github.com/cloudant/haengematte/tree/master/nodejs
https://github.com/cloudant/nodejs-cloudant#getting-started
https://github.com/cloudant/nodejs-cloudant#cloudant-search
var books = [
{author:"Charles Dickens", title:"David Copperfield"},
{author:"David Copperfield", title:"Tales of the Impossible"},
{author:"Charles Dickens", title:"Great Expectation"}
]
db.bulk({docs:books}, function(er) {
if (er) {
throw er;
}
console.log('Inserted all documents');
});
SQL MAX() and GROUP BY for CouchDB
在Cloudant的Design Document中新建一个叫ddoc
的document, 在建一个index name为skill的view(这个index name其实叫view name会更合适吧!)
我依照上篇博客写了个出来(只能用ES5)
function (doc) {
if(doc['Intranet ID']){
var skills = ['C','Java','Python','R'];
skills.forEach(function(skill) {
if(doc[skill]){
emit(skill, 1);
}
});
}
}
在线测试: https://xxx-bluemix.cloudant.com/wh_skills/_design/ddoc/_view/skill?group=true
使用node.js代码测试
db.view('ddoc', 'skill', {
// key: 'Java',
group: true
}, function (err, body) {
if (err) {
console.log(err);
} else {
body.rows.forEach(function (doc) {
console.log(doc);
});
}
});
结果
{ key: 'C', value: 111 }
{ key: 'Java', value: 166 }
{ key: 'Python', value: 136 }
{ key: 'R', value: 79 }
先按name分组再按level分组
function (doc) {
if(doc['Intranet ID']){
var cats = ['General Business','Banking','Travel & Transportation','Insurance','Electronics'];
cats.forEach(function(cat) {
if(doc[cat]){
emit([cat, doc[cat]], 1);
}
});
}
}
在线测试 https://xxx-bluemix.cloudant.com/wh_skills/_design/ddoc/_view/skill?group=true&group_level= (此时level可以是1或2, 默认为2)
可传的参数列表: https://console.bluemix.net/docs/services/Cloudant/api/using_views.html#using-views
level=1的结果如下:
{
"rows": [
{
"key": [
"Banking"
],
"value": 134
},
{
"key": [
"Electronics"
],
"value": 78
},
level=2的结果如下:
{
"rows": [
{
"key": [
"Banking",
"____10___"
],
"value": 1
},
{
"key": [
"Banking",
"___0~3_"
],
"value": 62
},
node.js测试代码
db.view('ddoc', 'industry', {
// 'include_docs': true,
group: true,
group_level: 1
}, function (err, body) {
if (err) {
console.log(err);
} else {
body.rows.forEach(function (doc) {
console.log(doc);
});
}
});
selector
各种操作符的详细文档 Query
文档在github上的地址(最新文档): https://github.com/IBM-Bluemix-Docs/Cloudant/blob/master/api/cloudant_query.md
$in
怎么构造复杂的查询条件, 比如and, or, in ... https://console.bluemix.net/docs/services/Cloudant/api/cloudant_query.html#creating-selector-expressions
not in
$nin
delete a document
delete也要用insert接口来操作, IBM这都能想出来, 实在是TMD的坑啊
模糊查询 $regex
全文检索
index
查看所有的索引: ACCOUNT.cloudant.com/DB_NAME/_index 新建索引 点Design Documents右边的加号, 选择Query Indexes, 会出现如下模板
修改fields部分如下:
然后查询时就可以按这两个字段排序了.
排序
点Query然后, 像下面这样组装一个查询条件, 然后就可以RUN了.
References
https://console.bluemix.net/docs/services/Cloudant/api/cloudant_query.html#query https://developer.ibm.com/clouddataservices/docs/cloudant/cloudant-query/ https://github.com/IBM-Bluemix-Docs/Cloudant/blob/master/tutorials/create_query.md https://www.ibm.com/developerworks/cn/data/library/techarticle/dm-1603-nosql-cloudant/index.html