简体   繁体   English

根据node.js中的日期减少couchbase

[英]couchbase reduce by date in node.js

I have to make some code which filter active user with couchbase, node.js 我必须编写一些代码,以使用沙发床node.js过滤活动用户

I have some users documents, and I made a view with the following coded : 我有一些用户文档,并且使用以下代码进行了查看:

I made a view called "bydate" with the following code : 我使用以下代码创建了一个名为“ bydate”的视图:

function (doc, meta) {  
    if(meta.type == 'json') { 
        if(doc.type == 'user') { 
           if (doc.lastUpdate){ 
                emit(dateToArray(doc.lastUpdate),doc.name); }
        }
    }
} 

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 我必须使用ouchbase控制台中的“ group_level”设置按日,月或年进行过滤,但是我无法在node.js端进行正确过滤

here is my node.js code 这是我的node.js代码

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

    }

the console output is : 控制台输出为:

{ key : null, value : XX}

I don't understand why I don't get the dateToArray result as key. 我不明白为什么我没有把dateToArray结果作为键。 any ideas ? 有任何想法吗 ?

Edit : 编辑:

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

I suspect you're using the 2.0.3 (latest) version of the node SDK, which ignores the params object you're passing to the query function, because it expects the ViewQuery object itself to specify the options. 我怀疑您正在使用节点SDK的2.0.3(最新)版本,该版本会忽略您传递给query函数的params对象,因为它希望ViewQuery对象本身可以指定选项。

Try this: 尝试这个:

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

Note that the range method takes the start key, end key, and inclusive end boolean flag. 请注意, range方法采用开始键,结束键和包含结束符的布尔值标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM