REST API - Query with explicit keys in Android

Hi,
we query a view about the REST API using the POST method with explicit keys.
Unfortunately, we do not get any result in couchbase mobile 1.3.1.
The same Query works fine with couchbase mobile 1.1.0

Additional Information:

  • CouchDB V. 2.0.0 server
  • create the designDocument also over REST API.

CURL:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ "keys": [ ["MENU", 1, 4, 4, 0], ["MENU", 1, 0, 0, 0], ["MENU", 1, 4, 4, 8], ["MENU", 1, 0, 0, 8] ] }' 'http://172.16.31.230:5984/servtouch962afde00917196b/_design/mainApp/_view/getMenu'

MAP FUNCTION:
function(doc) {
	if (doc.type && (doc.type === 'MENU')) {
		var mealId = 0;
		if (doc.mealId) {
			mealId = doc.mealId;
		}
		if (doc.type === 'MENU') {
			if (doc.stations.length === 1) {
				dietGrId = doc.stations[0];
			}
		}
		emit([doc.type, mealId, doc.week, doc.day, dietGrId], doc);
	}
};

DATA:
{
  "_id": "MENU_10",
  "_rev": "4-9a7d86f53125771a892d0b2d8189f75d",
  "week": 0,
  "day": 0,
  "grpsortno": 13,
  "component": {
    "alternatedesc": "Tee"
  },
  "compsortno": 7,
  "mealId": 1,
  "stations": [],
  "type": "MENU",
  "savedat": "20170126135918"
},
{
  "_id": "MENU_11",
  "_rev": "4-470fbcc5099a8e568f94ea410ecc7286",
  "week": 0,
  "pkid": 11,
  "day": 0,
  "grpsortno": 13,
  "component": {
    "alternatedesc": "Kakao"
  },
  "compsortno": 13,
  "mealId": 1,
  "stations": [],
  "type": "MENU",
  "savedat": "20170126135919"
}

On which platform (iOS, Android, .NET…)? Is the design document created + queried over the REST API of Couchbase Lite or on CouchDB?

The platform is android.
The design document is created and queried over the REST Api of couchbase lite by a PUT method.
I also tried to query the view over a browser, with cbm 1.1.0 it is possible, with 1.3.1 i get also no results.

see above my post:
Unfortunately, we do not get any result in couchbase mobile 1.3.1.
The same Query works fine with couchbase mobile 1.1.0

DESIGN DOCUMENT:

var docDesignMainApp = {
    "_id": "_design/mainApp",
    "language": "javascript",
    "views": {
        "getByTypeSorted": {
            "map": "function(doc) { if (doc.type) { var sortno = 0; var type = doc.type; if (doc.sortno) {sortno = doc.sortno;} if (type === 'TRAN') { type = type + '_' + doc.TRAN.PATIENT.name.toUpperCase(); } emit([type, sortno], doc); }}"
        },
        "getMenu": {
            "map": "function(doc) { if (doc.type && (doc.type === 'MENU')) { var dietGrId = 0; var mealId = 0; if (doc.mealId) { mealId = doc.mealId; } if (doc.dietGrId) { dietGrId = doc.dietGrId;  } if (doc.type === 'MENU') { if (doc.stations.length === 1) { dietGrId = doc.stations[0]; } } emit([doc.type, mealId, doc.week, doc.day, dietGrId], doc); } }"
        }
    },
    "filters": {
        "deviceFilter": "function(doc, req){if (doc._deleted) {return true;} if (doc.type && doc.type.lastIndexOf('OUT') == (doc.type.length - 3)) {return false;} if (doc.type && doc.type == 'SERVERADDRESS') {return false;} if (doc.deviceId && req.query.deviceId && doc.deviceId == req.query.deviceId) { return true; } if (doc.deviceUUID && req.query.deviceUUID && doc.deviceUUID == req.query.deviceUUID && doc.type == 'DEVICE') { return false; } if (doc.couchapp) { return false; } if (doc.deviceId && doc.deviceId == '*') { return true; } return false;}",
        "newsFilter": "function(doc, req){if (doc._deleted) {return false;} if (doc.type && doc.type == 'NEWS' ) { return true; } return false;}"
    },
    "couchapp": {
        "manifest": [
            "couchapp.json",
            "filters/",
            "filters/deviceFilter.js",
            "filters/newsFilter.js",
            "language",
            "views/",
            "views/getByTypeSorted/",
            "views/getByTypeSorted/map.js",
            "views/getMenu/",
            "views/getMenu/map.js"
        ],
        "signatures": {
        },
        "name": "Basic CouchApp",
        "objects": {
        },
        "description": "CouchApp with changes feed and form support."
    }
};
var optionsDesignMainApp = {
    dbName: that.deviceinfo.databasenamedevice,
    success: function (data) {
        callback(data);
    },
    error: function (status) {
        callback(status);
    }
};
saveSingleDoc(docDesignMainApp, optionsDesignMainApp);

It’s really hard to read your reply. Can you format the code properly and use triple back ticks:

```javascript
your code
```

no problem…
DATA:

{
  "_id": "MENU_10",
  "_rev": "4-9a7d86f53125771a892d0b2d8189f75d",
  "week": 0,
  "day": 0,
  "grpsortno": 13,
  "component": {
    "alternatedesc": "Tee"
  },
  "compsortno": 7,
  "mealId": 1,
  "stations": [],
  "type": "MENU",
  "savedat": "20170126135918"
},
{
  "_id": "MENU_11",
  "_rev": "4-470fbcc5099a8e568f94ea410ecc7286",
  "week": 0,
  "pkid": 11,
  "day": 0,
  "grpsortno": 13,
  "component": {
    "alternatedesc": "Kakao"
  },
  "compsortno": 13,
  "mealId": 1,
  "stations": [],
  "type": "MENU",
  "savedat": "20170126135919"
}

MAP FUNCTION:

function(doc) {
	if (doc.type && (doc.type === 'MENU')) {
		var mealId = 0;
		if (doc.mealId) {
			mealId = doc.mealId;
		}
		if (doc.type === 'MENU') {
			if (doc.stations.length === 1) {
				dietGrId = doc.stations[0];
			}
		}
		emit([doc.type, mealId, doc.week, doc.day, dietGrId], doc);
	}
};

CURL:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ "keys": [ ["MENU", 1, 4, 4, 0], ["MENU", 1, 0, 0, 0], ["MENU", 1, 4, 4, 8], ["MENU", 1, 0, 0, 8] ] }' 'http://172.16.31.230:5984/servtouch962afde00917196b/_design/mainApp/_view/getMenu'

i’m creating the DESIGN DOCUMENT also over REST, as well as in version 1.1.0:

    var docDesignMainApp = {
        "_id": "_design/mainApp",
        "language": "javascript",
        "views": {
            "getByStationRoom": {
                "map": "function(doc) { if (doc.type && doc.type == 'TRAN' && doc.TRAN.PATIENT.station && doc.TRAN.PATIENT.room ) { emit([doc.TRAN.PATIENT.station.id, doc.TRAN.PATIENT.room], doc); }}"
            },
            "getByPaidDiet": {
                "map": "function(doc) { if (doc.type && doc.type == 'TRAN' && doc.TRAN && doc.TRAN.DIET.pkid && doc.TRAN.DIET.diets) { for (var idx = 0; idx < doc.TRAN.DIET.diets.length; idx++) { emit([doc.TRAN.DIET.pkid, doc.TRAN.DIET.diets[idx].validFromNum, doc.TRAN.DIET.diets[idx].meal.sortno], {\"mealId\":doc.TRAN.DIET.diets[idx].meal.id, \"dietidx\":idx}); }  }  }"
            },
            "getByTypeSorted": {
                "map": "function(doc) { if (doc.type) { var sortno = 0; var type = doc.type; if (doc.sortno) {sortno = doc.sortno;} if (type === 'TRAN') { type = type + '_' + doc.TRAN.PATIENT.name.toUpperCase(); } emit([type, sortno], doc); }}"
            },
            "getMenu": {
                "map": "function(doc) { if (doc.type && (doc.type === 'MENU' || doc.type === 'MENUCOMPONENT' || doc.type === 'DIETMEALPROFIL')) { var dietGrId = 0; var mealId = 0; if (doc.mealId) { mealId = doc.mealId; } if (doc.dietGrId) { dietGrId = doc.dietGrId;  } if (doc.type === 'MENU') { if (doc.stations.length === 1) { dietGrId = doc.stations[0]; } } emit([doc.type, mealId, doc.week, doc.day, dietGrId], doc); } }"
            }
        },
        "filters": {
            "deviceFilter": "function(doc, req){if (doc._deleted) {return true;} if (doc.type && doc.type.lastIndexOf('OUT') == (doc.type.length - 3)) {return false;} if (doc.type && doc.type == 'SERVERADDRESS') {return false;} if (doc.deviceId && req.query.deviceId && doc.deviceId == req.query.deviceId) { return true; } if (doc.deviceUUID && req.query.deviceUUID && doc.deviceUUID == req.query.deviceUUID && doc.type == 'DEVICE') { return false; } if (doc.couchapp) { return false; } if (doc.deviceId && doc.deviceId == '*') { return true; } return false;}",
            "newsFilter": "function(doc, req){if (doc._deleted) {return false;} if (doc.type && doc.type == 'NEWS' ) { return true; } return false;}"
        },
        "couchapp": {
            "manifest": [
                "couchapp.json",
                "filters/",
                "filters/deviceFilter.js",
                "filters/newsFilter.js",
                "language",
                "views/",
                "views/getByPaidDiet/",
                "views/getByPaidDiet/map.js",
                "views/getByStationRoom/",
                "views/getByStationRoom/map.js",
                "views/getByTypeSorted/",
                "views/getByTypeSorted/map.js",
                "views/getMenu/",
                "views/getMenu/map.js"
            ],
            "signatures": {
            },
            "name": "Basic CouchApp",
            "objects": {
            },
            "description": "CouchApp with changes feed and form support."
        }
    };
    var optionsDesignMainApp = {
        dbName: that.deviceinfo.databasenamedevice,
        success: function (data) {
            callback(data);
        },
        error: function (status) {
            callback(status);
        }
    };
    saveSingleDoc(docDesignMainApp, optionsDesignMainApp);

thanks in advance!

It works fine in my testing:

  1. Start liteserv-java with CBL 1.3.1

  2. Install CORS proxy to allow sending request from the browser (needed in step below).

    npm install -g corsproxy
    corsproxy
    
    > 170202/173725.478, [log,info], data: CORS Proxy running at: http://localhost:1337
    
  3. Open http://codepen.io/Jamiltz/pen/ggejro?editors=1011 and run the following.

The view query is sent with {keys: [0]} and it returns 1 document out of 2 as expected. Feel free to fork and modify the Codepen to make it fit your scenario. So far, I don’t see any issue with that endpoint.

Your example works fine.
But the problem is querying with a complex key like that:
QUERY:

{ "keys":[[1,0]]}

VIEW like this:

"views": {
   "byMenu": {"map": "function(doc) { if (doc.type && (doc.type === 'MENU')) { emit([doc.week,doc.day], doc); } }"}
},

In this case, i get no result.

I just updated the Codepen and queried the view with [1,0] as the key. The result is returned as expected with 1.3.1. Have you tried isolating the problem to happen in Codepen? That would really help.

I have isolated the problem in a new Codepen.

my codepen

The indexer builds the keys with a wrong double value like 0.0.

After createDatabase, addDocs and updateDesignDoc please take a look at the view result.

for example:
http://localhost:59840/todo/_design/mainApp/_view/getMenu

this key is wrong:
“key”:[“MENU”,1,0,0,0.0]

Glad you managed to repro it there! Turns out it happens with non-array keys too, filed a ticket for it in the java-core repo https://github.com/couchbase/couchbase-lite-java-core/issues/1582.