我有Couchbase,其中文档以json格式存储 喜欢 QueueMessage / ABC123 QueueMessage / pqr456
以及QueueMessage / abc123
中的内容 {
"queue_list": {
"type": "cards.create",
"id": "abc123",
"userId": "abcd",
"deviceId": "abcd-1dg",
"status": "Accepted"
},
"id": "abc123"
}
和QueueMessage / pqr456
{
"queue_list": {
"type": "cards.create",
"id": "pqr456",
"userId": "pqr",
"deviceId": "abcd-78n",
"status": "Accepted"
},
"id": "pqr456"
}
我想使用userid和device id创建couchbase视图。我怎么能用红宝石做到这一点?
答案 0 :(得分:0)
require 'couchbase'
#Connect to qlists bucket:
client = Couchbase.new(
node_list: ['127.0.0.1:8091'],
bucket: 'qlists',
username: '7stud',
password: 'abc123'
)
#Enter some data in the qlists bucket:
client['QueueMessage/abc123'] = {
"queue_list": {
"type": "cards.create",
"id": "abc123",
"userId": "abcd",
"deviceId": "abcd-1dg",
"status": "Accepted"
},
"id": "abc123"
}
client['QueueMessage/pqr456'] = {
"queue_list": {
"type": "cards.create",
"id": "pqr456",
"userId": "pqr",
"deviceId": "abcd-78n",
"status": "Accepted"
},
"id": "pqr456"
}
#Create a view:
view_doc = %q[
{
"_id": "_design/qlists",
"language": "javascript",
"views": {
"userId_deviceId": {
"map": "function(doc){ emit(doc.queue_list.userId, doc.queue_list.deviceId); }"
}
}
}
]
client.save_design_doc(view_doc)
#Use the view to retrieve data:
qlists = client.design_docs['qlists']
qlists.userId_deviceId.each do |row|
puts "#{row.key}, #{row.value}"
end
--output:--
abcd, abcd-1dg
pqr, abcd-78n
我无法让reduce
在视图中工作。