Filter operators for collections (Go)
Many Data API commands, like Find documents (Go), accept a filter parameter to determine which documents to return or update. The filter parameter uses one or more filter operators.
You must use & to escape any . or & in field names in a filter.
Dot notation, which is used to reference nested fields, should not be escaped.
For more information, see Work with . and & in field names (Go).
Operators
You can use the following operators in a filter. All operators are case-sensitive.
Equal (default)
The $eq operator matches documents where the specified field is equal to the specified value.
You can’t use this operator on the $lexical field.
This is the default when you don’t specify an operator, except when you filter on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Eq("number_of_pages", 714)).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Not equal
The $ne operator matches documents where the specified field is not equal to the specified value.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Ne("number_of_pages", 300)).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Greater than
The $gt operator matches documents where the specified field is greater than the specified value.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Gt("number_of_pages", 300)).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Greater than or equal
The $gte operator matches documents where the specified field is greater than or equal to the specified value.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Gte("number_of_pages", 300)).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Less than
The $lt operator matches documents where the specified field is less than the specified value.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Lt("number_of_pages", 300)).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Less than or equal
The $lte operator matches documents where the specified field is less than or equal to the specified value.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Lte("number_of_pages", 300)).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
In
The $in operator matches documents where the field contains any of the specified values.
For fields that store an array, the operator matches documents where the array contains any of the specified values.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.In("genres", "Fantasy", "Romance")).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
If you have only one value to match, you can use the single value instead of an array, like { "$in": "VALUE" }.
Not in
The $nin operator matches documents where the field doesn’t contain any of the specified values.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(
ctx,
filter.Nin("genres", "Fantasy", "Romance"),
).Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Exists
The $exists operator matches documents that have the specified field, even if the field value is null.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Coll.Exists("borrower", true)).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
All
For fields that store an array, the $all operator matches documents where the array contains all of the specified values.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(
ctx,
filter.All("genres", "Fantasy", "Romance"),
).Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Size
For fields that store an array, the $size operator matches documents where the array has the specified number of elements.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Coll.Size("genres", 3)).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Not
The $not operator returns documents that don’t match the condition of the clause.
You can’t use this operator on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(
ctx,
filter.Coll.Not(filter.Eq("is_checked_out", false)),
).Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Match
|
Lexicographical matching is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
The $match operator finds documents whose $lexical field value is a lexicographical match to the specified string of space-separated keywords or terms.
You can only use the $match operator on the $lexical field.
This is the only operator that the $lexical field supports.
This is the default operator when you don’t specify an operator and filter on the $lexical field.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Coll.LexicalMatch("tree hill")).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Combine operators (And, Or)
The $and operator joins clauses with a logical AND.
Only documents that match the conditions of all clauses are returned.
The $or operator joins clauses with a logical OR.
Documents that match the condition of any of the clauses are returned.
You can use $and and $or separately or together.
If you want to match multiple flexible conditions, use $and and $or together in the same filter:
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(
ctx,
filter.And(
filter.Or(
filter.Eq("is_checked_out", false),
filter.Lt("number_of_pages", 300),
),
filter.Or(
filter.In("genres", "Fantasy", "Romance"),
filter.Gte("publication_year", 2002),
),
),
).Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
To filter on a range of values, use $and with $lt/$lte and $gt/$gte:
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(
ctx,
filter.And(
filter.Lt("number_of_pages", 300),
filter.Gte("number_of_pages", 200),
),
).Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
Filter nested fields
Use dot notation to filter nested fields.
For example, field.subfield.subsubfield.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Find a document
var result astra.Document
err := collection.FindOne(ctx, filter.Eq("metadata.language", "French")).
Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}