通过参数查询Couchbase视图

Querying Couchbase view by passing parameter

本文关键字:Couchbase 视图 查询 参数      更新时间:2023-09-26

我将Couchbase通知文档以以下格式存储:

{
    "_id":"notification::5403cb1a6c965dcfe9",
    "alert":"test data",
    "body":{
        "created_by":"user_54986e326694f10e511cf",
        "description":" reported ",
    },
    "channels":[
        "user_54986e326694f10e511cf",
        "user_54986e326694f10e511cp"
    ],
    "notification_type":"new_report",
    "seen":true,
    "timestamp":"2015-01-06T15:48:25.092Z",
    "type":"notification"
}

JSON文档包含channels数组,它是订阅频道的user_id的列表。问题是,如果通道字段包含user_id,我必须发出通知文档,如果我传递user_id ='user_54986e326694f10e511cf'作为关键参数。

下面是我的Couchbase视图映射函数,但它不返回任何东西

function (doc, meta) {
  if(doc.type=='notification' && doc.channels){
        for (var user in doc.channels) {
            emit(user, {
                alert: doc.alert,
                body: doc.body,
                seen: doc.seen,
                timestamp: doc.timestamp
            });
        }
    }
}

我想知道我哪里做错了。

这些"alert", "body", "seen"等,你发出在字典-这些是字符串吗?如果是这样,难道不应该引用它们吗?

换句话说,试试这个:

function (doc, meta) {
    if(doc.type=='notification' && doc.channels){
        for (var user in doc.channels) {
            emit(user, {
                "alert": doc.alert,
                "body": doc.body,
                "seen": doc.seen,
                "timestamp": doc.timestamp
            });
        }
    }
}