Duplicated documents with an index using multiple terms

Hi guys.

We’re trying to get multiple documents that have specified fields using an index with multiple terms. If values is specified as a specific field for the index, and some documents have different values in the field, Fauna returns duplicated documents.

For example,

  1. The collection name is Items , and the documents are the following.
{
  id: "ID_A",
  projectId: "PROJECT_ID_A",
  accounts: [
    { accountId: "ACCOUNT_ID_A", belongedAt: 1 },
    { accountId: "ACCOUNT_ID_B", belongedAt: 2 }
  ]
}

{
  id: "ID_B",
  projectId: "PROJECT_ID_A",
  accounts: [
    { accountId: "ACCOUNT_ID_A", belongedAt: 3 }
  ]
}
  1. Create an index like the following.
CreateIndex({
  name: "Items.allByProjectIdAndAccountId",
  source: Collection("Items"),
  terms: [
    { field: ["data", "projectId"] },
    { field: ["data", "accounts", "accountId"] },
  ],
  values: [
    { field: ["data", "accounts", "belongedAt"] },
    { field: ["ref"] },
  ],
});
  1. Query to get documents that have a specific projectId and a specific accounts.accountId .
Select(
  "data",
  Map(
    Paginate(Match(Index("Items.allByProjectIdAndAccountId"), ["PROJECT_ID_A", "ACCOUNT_ID_B"])),
    Lambda(["belongedAt", "ref"], Select("data", Get(Var("ref")))),
  ),
);
  1. We expect that the total number of the returned documents is 1, but Fauna returns two documents.
[
  {
    id: "ID_A",
    projectId: "PROJECT_ID_A",
    accounts: [
      { accountId: "ACCOUNT_ID_A", belongedAt: 1 },
      { accountId: "ACCOUNT_ID_B", belongedAt: 2 }
    ]
  },
  {
    id: "ID_A",
    projectId: "PROJECT_ID_A",
    accounts: [
      { accountId: "ACCOUNT_ID_A", belongedAt: 1 },
      { accountId: "ACCOUNT_ID_B", belongedAt: 2 }
    ]
  }
]
  1. If accounts.belongedAt of all documents are the same value, Fauna returns 1 document we expected.
[
  {
    id: "ID_A",
    projectId: "PROJECT_ID_A",
    accounts: [
      { accountId: "ACCOUNT_ID_A", belongedAt: 1 },
      { accountId: "ACCOUNT_ID_B", belongedAt: 1 }
    ]
  }
]

Is this a bug? or is there a mistake? What should we do?

When you index a field that is an array, Fauna creates one index entry per array item. See: Indexes | Fauna Documentation

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