From: Javier Sancho Date: Wed, 9 Mar 2016 16:25:26 +0000 (+0100) Subject: Basic searching support X-Git-Url: https://git.jsancho.org/?p=bongodb.git;a=commitdiff_plain;h=0fe9f925e00b6899f3b685082ed9b3374933c527 Basic searching support --- diff --git a/src/bongodb.scm b/src/bongodb.scm index 3cffa48..e4df14b 100644 --- a/src/bongodb.scm +++ b/src/bongodb.scm @@ -79,10 +79,23 @@ "Query the collection and return the documents that match with filter" (let ((table (get-table col))) (vhash-fold - (lambda (key value result) (cons (vhash->alist value) result)) + (lambda (key document result) + (cond ((match-document? document filter) + (cons (vhash->alist document) result)) + (else + result))) '() table))) +(define (match-document? document filter) + "Try to match the given document with a list of conditions" + (cond ((null? filter) + #t) + (else + (and + (equal? (vhash-assoc (caar filter) document) (car filter)) + (match-document? document (cdr filter)))))) + ;;; Tools diff --git a/tests/sample.scm b/tests/sample.scm index 047a751..899c6e0 100644 --- a/tests/sample.scm +++ b/tests/sample.scm @@ -30,5 +30,7 @@ ; Search (test-eqv 2 (length (find col '((a . 1))))) +(test-eqv 1 (length (find col '((a . 1) (b . 2))))) +(test-eqv 0 (length (find col '((a . "test"))))) (test-end "bongodb")