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,
- 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 }
]
}
- 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"] },
],
});
- Query to get documents that have a specific
projectId
and a specificaccounts.accountId
.
Select(
"data",
Map(
Paginate(Match(Index("Items.allByProjectIdAndAccountId"), ["PROJECT_ID_A", "ACCOUNT_ID_B"])),
Lambda(["belongedAt", "ref"], Select("data", Get(Var("ref")))),
),
);
- 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 }
]
}
]
- 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?