ostafen / clover

A lightweight document-oriented NoSQL database written in pure Golang.
MIT License
666 stars 55 forks source link

Add support for criteria involving operations between fields #49

Closed ostafen closed 2 years ago

ostafen commented 2 years ago

Currently, clover db only supports building criteria where you compare a field with a value, like the one in the following example:

db.Query("myCollection").Where(c.Field("myField").Eq(1))

Naturally, there are several contexts where one may need to express condition where you compare fields of the same document. For example you may want to select documents satisfying a condition such as myField1 > myField2.

This could be integrated inside clover in the following way:

db.Query("myCollection").Where(c.Field("myField1").Gt("$myField2"))

where the special syntax $myField2 tells clover to extract the value to use for comparison from the document field named myField2.

Suggestions and feedback about alternative ways to accomplish this are welcome :=)

segfault99 commented 2 years ago

Nice idea. I think this kind of syntax explains the intent better.

db.Query("myCollection").Where(c.Field("myField1").Gt(c.Field("myField2"))
ostafen commented 2 years ago

Yes, it is the best alternative :) I thought about this, and, to tell the truth, it is also the most natural way. But maybe, it could be useful to support the dollar syntax, which can be faster to use