include_docs=true的CouchDB视图产生doc:null

CouchDB view with include_docs=true yields doc: null

本文关键字:doc null 视图 CouchDB docs true include      更新时间:2023-09-26

我在同一个couchdb数据库中有两种不同的数据类型,messageuser。它们看起来如下:

// message
{
   "_id": "test__032142f981b1bddd00bb9a0161688480",
   "_rev": "8-d1b19caabac334c4b9e9eb1f0b0b8986",
   "created": "2011-12-27T23:48:17.940699Z",
   "text": "Omnis quam nesciunt dolor quaerat.",
   "author_id": "test__d3f62f343144dfef8fd112946da9c1b3",
   "type": "message",
   "subject": "Some text"
   "recipients": [
        "user id 1",
        "user id 2",
   ]
}
//user
{
   "_id": "test__d3f62f343144dfef8fd112946da9c1b3",
   "_rev": "12-88206492cc5274248aa5a64ff6a49b9a",
   "type": "user",
   "profile": {
       "callname": "User name",
       "fullname": "User name",
       "profile_text": "Blah blah",
       "urls": [
           {
               "url": "http://www.example.org",
               "description": "CV, Blog, Pictures, Research, Links",
               "label": "Homepage"
           }
       ],
       "profile_image_url": "http://localhost:5000/static/img/profiles/2e38f9fdd9e458eb4db0e7bb3b8e9576.jpg"
   },
   "registration_date": "2014-08-14T20:07:28Z",
   "verified": "none",
   "preferences": {
       "lang": "en"
   },
   "last_login": "2014-08-14T20:07:28Z",
   "email": "user@example.org"
}

出于某些原因,在这种情况下不可能进行内联。我现在想要的是获得包含author对象的所有消息的列表(如SQL SELECT * FROM messages JOIN users on message.author_id = message.id)。

我读过这两篇文章:

  • http://www.cmlenz.net/archives/2007/10/couchdb-joins
  • http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Linked_documents

所以我想使用include_docs=true,然后使用list来很好地格式化它。我的messages地图功能:

function(doc) {
    if (doc.type == 'message') {
        emit(doc._id, {_id: doc.author_id, entity: doc});
    }
}

我使用URL http://localhost:5984/dedri/_design/dedri/_view/messages?include_docs=true对此进行调用。但我明白了,doc: null:

{
    "total_rows": 34,
    "offset": 0,
    "rows": [
        {
            "id": "test__032142f981b1bddd00bb9a0161688480",
            "key": "test__032142f981b1bddd00bb9a0161688480",
            "value": {
                "_id": "test__d3f62f343144dfef8fd112946da9c1b3",
                "entity": {
                    "_id": "test__032142f981b1bddd00bb9a0161688480",
                    "_rev": "8-d1b19caabac334c4b9e9eb1f0b0b8986",
                    "created": "2011-12-27T23:48:17.940699Z",
                    "text": "Omnis quam nesciunt dolor quaerat.",
                    "version": "0.1",
                    "author_id": "test__d3f62f343144dfef8fd112946da9c1b3",
                    "type": "message",
                    "subject": "Some text"
                }
            },
            "doc": null
        },
        ...

我不知道该怎么办了。地图功能中没有进行编辑或删除。这是我的问题还是bug?

尝试将map js更改为更简单的形式进行调试,例如

function(doc) {
    if (doc.type == 'message') {
        // emit(doc._id, {_id: doc.author_id, entity: doc});
        emit(doc._id, { '_id': doc.author_id });
    }
}