uniquejava / blog

My notes regarding the vibrating frontend :boom and the plain old java :rofl.
Creative Commons Zero v1.0 Universal
11 stars 5 forks source link

cloudant #177

Open uniquejava opened 6 years ago

uniquejava commented 6 years ago

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

{
   "selector": {
      "type": "T_USER",
      "field2": {
         "$in": [
            "aaa",
            "bbb"
         ]
      }
   }
}

not in

$nin

delete a document

delete也要用insert接口来操作, IBM这都能想出来, 实在是TMD的坑啊

POST /database/_bulk_docs HTTP/1.1

{
    "docs": [
        {
            "_id": "some_document_id",
            "_rev": "1-6a466d5dfda05e613ba97bd737829d67",
            "_deleted": true
        }
        ...
    ]
}

模糊查询 $regex

{
    "selector": {
           "cast": {
            "$elemMatch": {
                "$regex": "^Robert"
            }
        }
    },
}

全文检索

{
    "selector": {
        "$text": "Bond"
    }
}

index

查看所有的索引: ACCOUNT.cloudant.com/DB_NAME/_index 新建索引 点Design Documents右边的加号, 选择Query Indexes, 会出现如下模板

{
   "index": {
      "fields": [
         "foo"
      ]
   },
   "name": "foo-json-index",
   "type": "json"
}

修改fields部分如下:

"fields": [
         {"field1": "desc"},
        {"field2":"desc"}
      ]

然后查询时就可以按这两个字段排序了.

排序

点Query然后, 像下面这样组装一个查询条件, 然后就可以RUN了.


{
   "selector": {
      "_id": {
         "$gt": "0"
      },
      "type": "AWESOME_TYPE"
   },
   "fields": [
      "_id",
      "_rev",
      "SORT_FIELD1",
      "SORT_FIELD2",
      "OTHER"
   ],
   "sort": [
      {
         "SORT_FIELD1": "desc"
      },
      {
         "SORT_FIELD1": "desc"
      }
   ],
   "limit": 2
}

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

uniquejava commented 6 years ago

The list of combination operators:

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.

java/node.js/python examples

所有语言的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

uniquejava commented 6 years ago

connect/insert

https://github.com/cloudant/nodejs-cloudant#getting-started

bulk insert

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');
});

map/reduce

Views (MapReduce)

SQL MAX() and GROUP BY for CouchDB

Design documents

类似于select name, count(name) from .. group by name

在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 }
uniquejava commented 6 years ago

complex keys (类似于group by COL1, COL2)

先按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);
    });
  }
});