Query View doesn't return any values in couchdb
I have a view defined as:
function(doc)
{
if (doc.type="user")
{
emit([doc.uid, doc.groupid], null);
}
}
In Java code, I have written
List<String> keys = new ArrayList<String>();
keys.add("93");
keys.add("23");
ViewQuery q = createQuery("getProfileInfo").descending(true).keys(keys).includeDocs(true);
ViewResult vr = db.开发者_Python百科queryView(q);
List<Row> rows = vr.getRows();
for (Row row : rows) {
System.out.println("Key--->"+row.getKey());
System.out.println("Value--->"+key);
}
My code always returns 0 rows - what have I missed?
I suspect you have a type mismatch, but it's impossible to tell for sure without seeing the view's rows. So, if I'm wrong please post an example row from your view.
'keys' is encoded to JSON before it is sent to CouchDB. You're adding two strings - "93" and "23" - but I'm guessing they're actually integers in the documents. In JSON, a string and an integer are encoded differently. A pair of strings is encoded to ["93", "23"] and a pair of integers is encoded to [93, 23].
If I'm correct then 'keys' should be defined as List<Integer> (or however that looks in Java).
Modified the code to
ComplexKey keys = ComplexKey.of(“93”,”23”);
ViewQuery q = createQuery("getProfileInfo").descending(true).key(keys).includeDocs(true);
ViewResult vr = db.queryView(q);
List<Row> rows = vr.getRows();
for (Row row : rows) {
System.out.println("Key--->"+row.getKey());
System.out.println("Value--->"+row.getValue());
}
and it works fine now.
精彩评论