06 Document Index Selector
Slice and dice a document list with one string of index math
- items_list
- selected_items
- document_id_links
- first_item
- first_document_id_link
- first_field_path_link
- first_file_path_link
- first_filename_link
- first_text_link
- selected_json
- indexed_selected_items
- count
- success
- status
Select Nth Item grabs one element; this is the version that grabs many. "06 Document Index Selector" takes a list and a little index expression and returns the subset you asked for - a single index, a range, a comma-separated set, or everything. It's the list-filtering workhorse for when you want to process documents 2–7, or just the ones at positions 0, 3, and 9, without any custom code.
How it works
The selection string is an index-expression compiler. The README's examples are the whole grammar:
3 → just index 3
2-7 → indices 2 through 7 inclusive
0,2,5 → that exact set
0,2-5,9 → mixed: 0, 2,3,4,5, and 9
* or all → the entire list
It parses that, applies it to items_list, and returns selected_items as a list plus selected_json (the JSON form), indexed_selected_items (numbered for reading), count, success, and status. The two toggles are worth knowing:
clamp_indexes(defaulttrue) - out-of-range indexes snap to the nearest valid item instead of being droppeddedupe(defaulttrue) - duplicate selections collapse while preserving order
And like its single-item sibling, it has semantic_hint (default document_id) driving the guarded link outputs - document_id_links (a list of typed links), first_document_id_link, plus first_field_path_link / first_file_path_link / first_filename_link / first_text_link for whichever value type you're slicing. first_item gives you the first selected value as a plain string.
Install
cd ComfyUI/custom_nodes
git clone https://github.com/CentralFloridaAttorney/ComfyUI-ZMongo
Restart, or install "ComfyUI-ZMongo" via ComfyUI Manager.
Where people get burned
Spacing and syntax: 2 - 7 with spaces parses less reliably than 2-7, and the docs' canonical examples are unspaced. Keep your expressions tight and you won't have to debug them. Also, ranges are inclusive - 2-7 is six items, not five. If you're coming from Python slicing where [2:7] excludes 7, unlearn that here.
The other trap is what's in the list. This selector doesn't care whether items_list holds IDs, filenames, or paths - it just slices strings. If you feed it filenames and then wire first_document_id_link into a Get node, you'll get a miss (the guarded links try to prevent exactly this, but only if the hint matches the data). Feed it document_ids from List Documents, set semantic_hint to document_id, and the intended loop - List → Index Selector → Get Text - works cleanly.
And a minor one: the fallback string covers the empty/no-match case, so you get a graceful empty result rather than a crash. That's friendly for automation, but it also means an empty selected_items is worth reading as a signal that your expression or list is wrong, not just "no results."
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| items_list | * | — | |
| selection | STRING | 0 | Index expression: 0, 2-7, 0,2,5, 0,2-5,9, *, or all. |
| fallback | STRING | Returned only when the input list is empty or no selection matches. | |
| clamp_indexes | BOOLEAN | true | Clamp out-of-range indexes to the nearest valid item instead of dropping them. |
| dedupe | BOOLEAN | true | Remove duplicate selected items while preserving order. |
| semantic_hint | COMBO | document_id | Controls the guarded semantic link outputs. |
| refresh_tokenopt | STRING | — |
Outputs (13)
| Name | Type | Description |
|---|---|---|
| selected_items | * | — |
| document_id_links | ZMONGO_DOCUMENT_ID | — |
| first_item | STRING | — |
| first_document_id_link | ZMONGO_DOCUMENT_ID | — |
| first_field_path_link | ZMONGO_FIELD_PATH | — |
| first_file_path_link | ZMONGO_FILE_PATH | — |
| first_filename_link | ZMONGO_FILENAME | — |
| first_text_link | ZMONGO_TEXT | — |
| selected_json | STRING | — |
| indexed_selected_items | STRING | — |
| count | INT | — |
| success | BOOLEAN | — |
| status | STRING | — |