根据node.js中的日期减少couchbase [英] couchbase reduce by date in node.js
问题描述
我必须编写一些代码,以使用沙发床node.js过滤活动用户
I have to make some code which filter active user with couchbase, node.js
我有一些用户文档,并且使用以下代码进行了查看:
I have some users documents, and I made a view with the following coded :
我用以下代码创建了一个名为"bydate"的视图:
I made a view called "bydate" with the following code :
function (doc, meta) {
if(meta.type == 'json') {
if(doc.type == 'user') {
if (doc.lastUpdate){
emit(dateToArray(doc.lastUpdate),doc.name); }
}
}
}
我必须使用ouchbase控制台中的"group_level"设置按日,月或年进行过滤,但是我无法在node.js端进行正确过滤
I have to filter by day, month or year using the "group_level" setting in couchbase console , however I have unable to filter it properly on node.js side
这是我的node.js代码
here is my node.js code
router.get("/activetodaycount",
function(request,response,next)
{
var couchbase = require('couchbase');
var ViewQuery = couchbase.ViewQuery;
var params =
{
'reduce' : true
, 'group_level' : 3
, 'connection_timeout' : 600000
, 'limit' : 10
, 'skip' : 0
, 'stale' : false
, 'inclusive_end' : false
};
var query = ViewQuery.from('users', 'bydate')
couch.query(query, params, function(error, data)
{
if (error)
{
return next(error);
}
console.log(data)
var out = [];
for( var i = 0;i<data.length; i++ )
{
console.log(data[i])
var user = data[i].id;
out.push(user)
}
response.json({'cnt':out.length});
});
}
控制台输出为:
{ key : null, value : XX}
我不明白为什么我没有把dateToArray结果作为键.有什么想法吗?
I don't understand why I don't get the dateToArray result as key. any ideas ?
这是沙发床控制台中过滤结果的输出:
here is the output from filter result in the couchbase console:
Key Value
[2015,1,2,13,56,0]
user:0af1144d-a23b-4098-9079-13dea8eb24b1 null
[2015,1,2,13,56,0]
user:58bc2ca5-9c50-4a5f-a056-e2a7b32450fd null
[2015,1,2,16,43,57]
user:6b5f1c46-bf1d-43d6-bf25-b3f1a2495cdf null
推荐答案
我怀疑您正在使用节点SDK的2.0.3(最新)版本,该版本会忽略要传递给query
函数,因为它希望ViewQuery
对象本身指定选项.
I suspect you're using the 2.0.3 (latest) version of the node SDK, which ignores theparams
object you're passing to thequery
function, because it expects theViewQuery
object itself to specify the options.
尝试一下:
var couchbase = require('couchbase');
var ViewQuery = couchbase.ViewQuery;
var cluster = new couchbase.Cluster('localhost');
var bucket = cluster.openBucket('default', console.log);
var query = ViewQuery.from('users', 'bydate')
.group_level(6)
.stale(ViewQuery.Update.BEFORE)
.limit(10)
.range([2015,1,2,13,56,0], [2015,1,2,16,43,57], true);
bucket.query(query, console.log);
请注意,range
方法采用开始键,结束键和包含结束符的布尔值.
Note that therange
method takes the start key, end key, and inclusive end boolean flag.
这篇关于根据node.js中的日期减少couchbase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!