我正在尝试为我的NoSQL数据库实现正确的查询,但我很困惑如何在Cloudant中执行此操作。在SQL中有
SELECT * FROM mydb
WHERE user_permit_doc_id = 10
AND user_tracking_id = 1
我的查询如下:
https://293e2cb7-3561-4004-a1c3-58d54f517ee6-bluemix.cloudant.com/user_files/_design/user_tracking/_view/new-view?startkey=["user_permit_doc_id:10"]
并返回所有文档,而不仅仅是具有此ID的文档。
这是View的Map Reduce函数
function(doc) {
if(doc.user_tracking_id !== null){
emit(doc);
}
}
我的docs数据库中的doc示例
{
"_id": "6e57baa78c6415beeee788bc786cc53a",
"_rev": "5-f15352bce99c307bd246bda4dc0da75a",
"user_tracking_id": "1",
"user_permit_id": "2",
"user_permit_doc_id": "10",
"user_id": "1",
"_attachments": {
"6y41j4i68cic.jpg": {
"content_type": "image/jpeg",
"revpos": 2,
"digest": "md5-KC+G5tbz2UWZSzlPHvBy/Q==",
"length": 68367,
"stub": true
}
}
}
答案 0 :(得分:5)
您可以将视图更改为
function(doc) {
if(doc.user_tracking_id !== null){
emit([user_tracking_id, user_permit_doc_id]);
}
}
然后使用复合键[1,10]
进行查询答案 1 :(得分:4)
如果您只想返回该特定文档,则必须将Starting Sauce Connect
25.96s$ travis_start_sauce_connect
Using temp dir /tmp/sc.hHct
/tmp/sc.hHct ~/build/Diobox/diobox_stateful
Downloading Sauce Connect
--2015-06-11 17:13:21-- http://saucelabs.com/downloads/sc-4.3.6-linux.tar.gz
Resolving saucelabs.com (saucelabs.com)... 162.222.73.28
Connecting to saucelabs.com (saucelabs.com)|162.222.73.28|:80... connected.
HTTP request sent, awaiting response... 200 OK
合并到地图缩减功能中,如下所示:
11 Jun 17:13:23 - Starting up; pid 3524
11 Jun 17:13:23 - Command line arguments: sc-4.3.6-linux//bin/sc -i 25.1 -f sauce-connect-ready-29337 -l /home/travis/sauce-connect.log
11 Jun 17:13:23 - Using no proxy for connecting to Sauce Labs REST API.
11 Jun 17:13:23 - Resolving saucelabs.com to 162.222.73.28 took 1 ms.
11 Jun 17:13:24 - ***********************************************************
11 Jun 17:13:24 - A newer version of Sauce Connect (build 1671) is available!
11 Jun 17:13:24 - Download it here:
11 Jun 17:13:24 - https://saucelabs.com/downloads/sc-4.3.8-linux.tar.gz
11 Jun 17:13:24 - ***********************************************************
11 Jun 17:13:24 - Started scproxy on port 50965.
11 Jun 17:13:24 - Please wait for 'you may start your tests' to start your tests.
11 Jun 17:13:24 - Starting secure remote tunnel VM...
11 Jun 17:13:24 - Shutting down named tunnel 25.1 (b7f004bfd8174ac6be612082b1654367).
11 Jun 17:13:32 - Secure remote tunnel VM provisioned.
11 Jun 17:13:32 - Tunnel ID: 6f295a34dfe14b639b78438e59307ae3
11 Jun 17:13:32 - Secure remote tunnel VM is now: booting
11 Jun 17:13:46 - Secure remote tunnel VM is now: running
11 Jun 17:13:46 - Remote tunnel host is: maki78107.miso.saucelabs.com
11 Jun 17:13:46 - Using no proxy for connecting to tunnel VM.
11 Jun 17:13:46 - Resolving maki78107.miso.saucelabs.com to 162.222.78.107 took 8 ms.
11 Jun 17:13:46 - Starting Selenium listener...
11 Jun 17:13:46 - Establishing secure TLS connection to tunnel...
11 Jun 17:13:46 - Selenium listener started on port 4445.
11 Jun 17:13:47 - Sauce Connect is up, you may start your tests.
11 Jun 17:13:47 - Connection established.
但是,既然你是从SQL过来的,那么你可能会更喜欢类似Mongo的查询。如果查询数据库的方式更适合您,请查看Cloudant Mango API layer。这个API向NoSQL引入类似SQL的查询,实际上在幕后创建了map reduce函数。