Xpages: get count of values in multi-value field i

2019-09-18 03:24发布

I have 100 documents with a multi-value field. The field holds 5 possible values (Albert,Ben,Chris,Don,Ed) let's say. The field must contain 1 value, but can contain up to 5.

I need to compute the number of docs that contain each value, so

Albert 56
Ben    22
Chris  79 

etc.

This seemed easy. I constructed a view that contains the docs, the first column is the field, and I selected show multiple documents for multiple feeds.

In SSJS loop through my master list of values in the field, and for each one do a getDocumentByKey.

myArray = applicationScope.application;     
var dc:NotesDocumentCollection;

for (index = 0; index < myArray.length; ++index) {
      dc = view1.getAllDocumentsByKey(myArray[index]);  
   Print(dc.getCount())
}

This gets the first value correctly, but none after. If I just hard code a particular value, it works. But when I call the getAllDocumentsByKey a second time, it doesn't return the right value.

I think this would work fine in LS, but in SSJS I must clear or recycle or rest something, but I don't know what.

4条回答
Explosion°爆炸
2楼-- · 2019-09-18 03:49

You will never get the right answer with getAllDocumentsByKey(). When document shows in one category, it will be missing from the collection of next category. That's the way it works.

Use ViewNavigator instead. Build it by category and simply count ViewEntries.

查看更多
祖国的老花朵
3楼-- · 2019-09-18 03:53

Are any of your multi-value field values ambiguous? getAllDocumentsByKey(Vector) does partial matches. Unless you ever want that, I would recommend always using the second parameter and setting it to true, same always for getAllEntriesByKey().

An alternative, which will definitely perform better, would be to add a total column to the view. There's a performance hit of that on the view indexing, but you can then use a ViewNavigator with getColumnValues() and getNextSibling(). getCount() is extremely poor performing in LS and will almost certainly be as poorly performing in SSJS/Java. See this blog post from a few years ago http://www.intec.co.uk/why-you-shouldnt-count-on-a-notesviewnavigator/

查看更多
等我变得足够好
4楼-- · 2019-09-18 03:53

If I understand that right: you want to count all values that are possible over an amount of documents to have a 5 value list with corresponsing 5 value counts, right? You could loop through all docs, loop through all values and add entries to a HashMap with the value as key and an int as value (which should be increased everytime). Afterall you have a Map with 5 values holding the sums of each keys.

查看更多
欢心
5楼-- · 2019-09-18 04:05

Bryan,

Two things to try in this order:

  1. Your first line of myArray = applicationScope.application; doesn't look right. I suspect that you are not actually getting an array here. I think you are just getting the first value from your applicationScope. Add a print statement on the next line of print(myArray.length); If it is always equal to one, that is your problem. You are not using var and you should set the variable to some java type using a colon.
  2. Before the end of your for loop, try setting your collection to null. dc = null; This way you know for sure that you are getting a new collection in the next iteration.
查看更多
登录 后发表回答