1

I have an existing design document. I want to import this design view using curl POST command. so that I can view this in fauxton.

{
    "total_rows": 1,
    "offset": 1,
    "rows": [
        {
            "id": "_design/editor",
            "key": "_design/editor",
            "value": {
                "rev": "1-c74604129e122055f0b58760a7e08ed8"
            },
            "doc": {
                "_id": "_design/editor",
                "_rev": "1-c74604129e122055f0b58760a7e08ed8",
                "language": "javascript",
                "views": {
                    "all_vendors": {
                        "map": "function(doc) {\n  if (doc.type==\"vendor\"){\n  emit(doc.name, doc.name);\n }\n}"
                    }
                }
            }
        }
    ]
}

I am trying to add this document in existing db with below command

curl -X POST http://127.0.0.1:5984/${db_name}/_design/${design_name}/_view/${view_name}
Abhinandan prasad
  • 1,009
  • 7
  • 13
  • POST executes the view. Creating a _design document is no different than creating any other document. – RamblinRose Feb 10 '20 at 13:37
  • @RamblinRose I tried the approach from the link you mentioned. I am getting error during PUT command ```{ "error": "conflict", "reason": "Document update conflict." }``` – Abhinandan prasad Feb 10 '20 at 13:50
  • 1
    Well that means the design document exists. It's not clear what is meant by "unable to see this view". Is the question that no results are being returned from the view, or that you want to view the design doc? If the latter, `curl -X GET http://127.0.0.1:5984/${db_name}/_design/${design_name}` should work. – RamblinRose Feb 10 '20 at 13:58
  • Design documents are not present in that database. It's a simple question where I am unable to add a new view in the database using the curl command. and the command I mentioned is not working. – Abhinandan prasad Feb 10 '20 at 15:39
  • the simplest way to diagnose this is to fire up Fauxton and have a look. `409 Conflict` is the red flag. Good luck – RamblinRose Feb 10 '20 at 15:58
  • To add a new view, you need to either create a new design document with your view or update an existing design document and add your view to it. To create a document, follow this [documentation](https://docs.couchdb.org/en/stable/api/database/common.html#post--db). Then, you can query your view with [this example](https://docs.couchdb.org/en/stable/api/ddoc/views.html#post--db-_design-ddoc-_view-view) – Alexis Côté Feb 10 '20 at 16:05
  • I want to add a design document with the view in it. This example won't help. – Abhinandan prasad Feb 11 '20 at 08:58

1 Answers1

4

You're not showing the actual doc you're trying to upload, and you're using a POST, so I'll have to guess a bit what you're doing. You're saying the doc you're wanting to upload does not exist in the database, so let's use a PUT instead, and remove any revs that may reside in the document itself:

% cat ddoc.json 
{"_id":"_design/editor","views":{"all_vendors":{"map":"function (doc) {\n  if (doc.type==\"vendor\"){\n    emit(doc.name, doc.name);\n  }\n}"}},"language":"javascript"}

So there's your view: note that there is no _rev field. This is crucial. Let's curl that to the database (I'm using Cloudant, but the same thing should work for couchdb):

% acurl -XPUT 'https://skruger.cloudant.com/source/_design/editor' -d@ddoc.json
{"ok":true,"id":"_design/editor","rev":"3-42790f55c52a203d1e83e0e94c2664a0"}

I can now see that view in fauxton just fine:

Fauxton showing uploaded ddoc view

and the named view itself:

enter image description here

xpqz
  • 3,617
  • 10
  • 16
  • The design document has been added after removing the ```rev``` tag. but in the design document, I should able to see the view with name ```all_vendors``` which I still not able to see it. the ```all_vendor``` should come in view tab under the design document. – Abhinandan prasad Feb 11 '20 at 13:18
  • 1
    It certainly shows up in my database -- I've added another screenshot to the post above. – xpqz Feb 11 '20 at 13:23
  • Hi, Thanks for the response. It worked now **I just need to pass the doc tag data with JSON** However, I have two queries 1. why do we need to remove `rev` tag. 2. how do we get the design document content from the existing DB..Thanks in advance :) – Abhinandan prasad Feb 11 '20 at 13:33
  • 1
    It's how the MVCC in CouchDB works -- if you pass a rev, you're saying that you want to update that exact revision of the doc. So if you're taking a doc from an existing db, it has a rev. But if you then try to store that in a new db, or one where the id of this doc isn't present, you get a conflict, as the rev doesn't exist there yet. If you want to lift & shift an existing ddoc with curl, you have to remove the _rev, of use a bulk_docs call with new_edits=false -- but that's deep magic terrain. – xpqz Feb 11 '20 at 14:00
  • when we do bulk_docs it doesn't differentiate between design_doc and normal doc. it just adds all doc as normal. – Abhinandan prasad Feb 11 '20 at 14:07
  • There is no distinction between design docs and any other doc, only the _id. Show me the exact curl command you used. Note that the body for the bulk_doc is no longer curlable as just @ddoc.json, as you need to enclose it in a {docs=[...]}. Note also that doing it this way (with a new_edits=false) can leave you with a very broken database as it's forcing the target to accept the incompatible revs. – xpqz Feb 11 '20 at 14:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207600/discussion-between-abhinandan-prasad-and-xpqz). – Abhinandan prasad Feb 11 '20 at 14:23