Search code examples
c#faunadb

Using the C# FaunaDB Client, How do I query an index?


I'm a newbie to FaunaDB. I've been able to add a bunch of documents to a collection using C# like this:

Value value = fauna.Query(Create(
    Collection("MyCollection"),
    Obj("data", Encoder.Encode(trans))
    )).Result;

Now I need to query individual documents. So in the FaunaDB Shell, this works great and returns the expected document.

Get(
  Match(
    Index("FITID"),
    "2021043000000000000000000000002122180019216600000088701"
  )
)

However, when I try to use this in C#, I get an error that I can't figure out how to fix. I can't find any examples specific to what I'm trying to do.

Value value = fauna.Query(
        Get(
            Match(
                Index("FITID", "2021043000000000000000000000002122180019216600000088701")
                )
            )
        ).Result;

The error I'm getting is

BadRequest: invalid argument: Database Ref expected, String provided.

Any suggestions?

EDIT: Thanks to @eskwayrd for spotting my error. The correct command is below.

Value value = fauna.Query(
        Get(
            Match(
                Index("FITID"), "2021043000000000000000000000002122180019216600000088701"
                )
            )
        ).Result;

Solution

  • The Index function returns a reference for the specified index name. In your C# code, you are passing both the index name and the numeric string to Index. If you fix the brackets in your query, it should work just fine.

    See the second example under "Retrieve Posts": https://docs.fauna.com/fauna/current/tutorials/crud?lang=csharp#retrieve