Hey @unverifieduser, welcome to the forum!
I’m assuming you’re talking about the records key, but you could follow the same setup for a map key as well, with a little extra setup.
To achieve this you would need to do a full scan, or secondary index query, but you could utilize a filter expression, RegexCompare
to be exact, to compare the key with the pattern. One caveat to this, the records would need to have been written with the sendKey
write policy set to true.
Here is a code sample, in Go, that walks through writing a record and then using a query with an expression filter to return based on a pattern to match the key. You could change things up to utilize a background query for the deletion.
You can drop this code directly into the sandbox on our Developer Hub as well. Just select Go as the language and hit Connect
, the paste and run this on the blank code tab. Hope that helps!
// Import Aerospike client library
import (
"log"
as "github.com/aerospike/aerospike-client-go/v6"
)
func writeData() {
client, errClient := as.NewClient("127.0.0.1", 3000)
if errClient != nil {
log.Printf("Error: %s", errClient)
}
// Close the connection to the server
defer client.Close()
// Initialize a write policy
policy := as.NewWritePolicy(0,0)
policy.SendKey = true
// Create a key in namespace "sandbox" and set "ufodata"
key, errKey := as.NewKey("sandbox", "ufodata", "myKeyString")
if errKey != nil {
log.Printf("Error: %s", errKey)
return
}
// Create the bins as Bin("binName", value)
myBin := as.NewBin("myBin", "we did it!")
// Write the record to Aerospike
errWrite := client.PutBins(policy, key, myBin)
if errWrite != nil {
log.Printf("Error: %s", errWrite)
return
}
log.Println("One record added")
}
func getData(){
client, errClient := as.NewClient("127.0.0.1", 3000)
if errClient != nil {
log.Printf("Error: %s", errClient)
return
}
// Close the connection to the server
defer client.Close()
// Create query policy
queryPolicy := as.NewQueryPolicy()
queryPolicy.FilterExpression = as.ExpRegexCompare(
".*Str.*",
as.ExpRegexFlagICASE,
as.ExpKey(as.ExpTypeSTRING))
// Create statement
stmt := as.NewStatement("sandbox", "ufodata")
// Execute the query
recordSet, err := client.Query(queryPolicy, stmt)
if err != nil {
log.Printf("Error: %s", err)
return
}
// Get the results
for records := range recordSet.Results() {
if records != nil {
// Do something
log.Printf("Record: %v \n", records.Record.Bins)
}
}
recordSet.Close()
}
func main() {
writeData()
getData()
}