Query all documents with index NOT EQUAL to "x"

I’m using the python driver.
Is there a way to query all documents where the index is not equal to a particular value?

for example, for returning all documents in a collection that with KeyIndex = “blah”

 q.map_(
        q.lambda_("x", q.get(q.var("x"))),
        q.paginate(q.match(q.index("my_index"),"blah"))
        )  

What if I want to return the opposite?
maybe there is an FQL function for this?

for example q.notEqual(“blah”)

 q.map_(
        q.lambda_("x", q.get(q.var("x"))),
        q.paginate(q.match(q.index("my_index"),q.notEqual("blah")))
        )  

The Match function only performs a strict equivalence match: you can’t adjust it to perform some other kind of match.

Ideally, you could use the Difference function to find the set of documents that does not match the indexed documents. For example:

q.map_(
  q.lambda_("x", q.get(q.var("x"))),
  q.paginate(
    q.difference(
      q.documents(q.collection("some_collection")),
      q.match(q.index("my_index"),"blah")
    )
  )
)  

That works reasonably well only for small collections. Large collections could cause your query to time out and never provide a result.

If you know the term ahead of time, you can create an index binding that can compute an indexable value however you like:

q.create_index({
  name: "my_index2",
  source: {
    collection: q.collection("some_collection"),
    fields: {
      hasBlah: q.query(
        q.lambda_(
          "doc",
          q.equals(q.select(["data", "field"], ""), "blah")
        )
      ),
    },
  },
  terms: [
    { binding: "hasBlah" }
  ],
})

With that index, you query would look like:

q.map_(
  q.lambda_("x", q.get(q.var("x"))),
  q.paginate(q.match(q.index("my_index2"), False))
)  

If you don’t know the term ahead of time, you’ll effectively need to perform a full scan of the collection, issuing multiple queries to paginate through the entire set of documents, filter the pages (and dealing with potentially empty pages), composing the final result set.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.