Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doclite built in functions #21

Closed
REDAL opened this issue Aug 31, 2022 · 7 comments
Closed

doclite built in functions #21

REDAL opened this issue Aug 31, 2022 · 7 comments

Comments

@REDAL
Copy link

REDAL commented Aug 31, 2022

Hi Doclite,

thank you for the awesome library,

is there any built-in function for example to get the count of documents in a collection, and such functions? (like the last id)

if not, is there any high-performance solution to do it, rather than querying all the documents and calculating the array count

regards

@REDAL
Copy link
Author

REDAL commented Sep 1, 2022

@dwgebler @Awilum @iGusev

any idea here ?

also, what if we want to do a search with an array, like sleek DB for example

$collection->where([[['__id', '!=', ""], "OR", ['role', '=', "admin"]], "AND", ['age', '<', "50"]])->fetch()

how will be the scenario with doclite ?

regards

@dwgebler
Copy link
Owner

dwgebler commented Sep 1, 2022

@REDAL in answer to your first question, yes the query builder has a count() method, e.g.

$numUsers = $users->where('active', '=', true)->count();

In regards to your second question, you can't use an array as search criteria but you chain conditions on the query builder as much as you like using and() and or() functions, e.g.:

$users->where('registered', '=', true)->and('active', '=', false)->fetch();

Please see the advanced queries section of the documentation

https://github.com/dwgebler/doclite#advanced-queries

@REDAL
Copy link
Author

REDAL commented Sep 2, 2022 via email

@dwgebler
Copy link
Owner

dwgebler commented Sep 2, 2022

@REDAL there is an example in the advanced queries section of the documentation - if I understand correctly what you're trying to do, you want the union() and intersect() functions which allow you to group clauses. An example of an efficient count with grouped clauses:

/ / Get the total number of users where (active=true AND email != "") OR (active=false AND email == "")
$numUsers = $users->where('active', '=', true)->and('email, 'NOT EMPTY')->union()->where('active', '=', false)->and('email', 'EMPTY)->count(); 

union() groups the clauses on either side by OR,intersect() groups the clauses on either side by AND.

The combination of all query functions, where(), and(), or(), union() and intersect() can be chained as much as you like to create whatever query you need.

@REDAL
Copy link
Author

REDAL commented Sep 2, 2022 via email

@REDAL
Copy link
Author

REDAL commented Sep 2, 2022

@dwgebler it worked like a charm :)

what about getting the last ID? , is there any built-in function , or how you do it

regards

@REDAL REDAL changed the title not creating new document doclite built in functions Sep 2, 2022
@dwgebler
Copy link
Owner

dwgebler commented Sep 2, 2022

There isn't any concept of a "last ID" in DocLite since the ID can be any unique string value. If you want to know the ID of a newly saved document, you can just call $document->getId() after saving it. Otherwise if you want to know the ID of the last (meaning most recent) document in a collection, you need to save that information yourself by e.g. adding a field to that document to record the DateTime it was created. Additionally, a DateTime can be extracted from auto generated IDs by calling the getTime() method on a document, see https://github.com/dwgebler/doclite#document-unique-id but there isn't a way to query by that, since a document ID doesn't have to be a UUID as long as it is something unique.

@dwgebler dwgebler closed this as completed Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants