Skip to content

Commit

Permalink
Adding support for arrayFilters in updates
Browse files Browse the repository at this point in the history
This PR is for the feature request [#4247](#4247).

This PR adds support for arrayFilters by passing the arrayFilters parameter to the Realm serverless function.

Additional testing was added to cover the case of an update with arrayFilters.
  • Loading branch information
joellord authored and kraenhansen committed Jan 26, 2022
1 parent d28bb6d commit eb4ad71
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,8 @@ class MongoDBCollection {
* @param {object} [options] Additional options to apply.
* @param {boolean} [options.upsert=false] if true, indicates that MongoDB should insert a new document that matches the
* query filter when the query does not match any existing documents in the collection.
* @param {object[]} [options.arrayFilters=false] if provided, indicates the arrayFilters to use to update
* an embedded array
* @returns {Promise<Realm.MongoDBCollection~UpdateResult>}
*/
updateOne(filter, update, options) {}
Expand All @@ -1291,6 +1293,8 @@ class MongoDBCollection {
* @param {object} [options] Additional options to apply.
* @param {boolean} [options.upsert=false] if true, indicates that MongoDB should insert a new document that matches the
* query filter when the query does not match any existing documents in the collection.
* @param {object[]} [options.arrayFilters=false] if provided, indicates the arrayFilters to use to update
* an embedded array
* @returns {Promise<Realm.MongoDBCollection~UpdateResult>}
*/
updateMany(filter, update, options) {}
Expand Down
2 changes: 2 additions & 0 deletions lib/mongo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class MongoDBCollection {
query: filter,
update,
upsert: options.upsert,
arrayFilters: options.arrayFilters,
});
}

Expand All @@ -194,6 +195,7 @@ class MongoDBCollection {
query: filter,
update,
upsert: options.upsert,
arrayFilters: options.arrayFilters,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,34 @@ describe("Remote MongoDB", () => {
expect(result.modifiedCount).equals(3);
});

it("can update documents using array filters", async () => {
const collection = getCollection<TestDocument>();
// Insert a document with an embedded array
const insertResult = await collection.insertOne([
{ runId, name: "arrayFilter", values: [ {condition: 1, status: false}, {condition: 2, status: false} ] },
]);
expect(insertResult.insertedIds.length).equals(1);

// Update the array element with condition == 1 to have status == true
let filter = { runId, name: "arrayFilter" };
let update = {"$set": {"values.$[element].status": true}};
let arrayFilters = [{"element.condition": 1 }];
const result = await collection.updateOne(filter, update, { arrayFilters });
// Check the result
expect(typeof result).equals("object");
expect(result.matchedCount).equals(1);
expect(result.modifiedCount).equals(1);

// Fetch the document to ensure that only the filtered elements of the array were updated
const doc = await collection.findOne({ runId, name: "arrayFilter" });
if (doc === null) {
throw new Error("Expected a result");
} else {
expect(doc.values.find(e => e.condition === 1).status).equals(true);
expect(doc.values.find(e => e.condition === 2).status).equals(false);
}
});

it("upserts when updating many non-existing documents", async () => {
const collection = getCollection<TestDocument>();
// Delete the document with the last name (Charlie)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export class MongoDBCollection<T extends Document> implements Realm.Services.Mon
query: filter,
update,
upsert: options.upsert,
arrayFilters: options.arrayFilters,
});
}

Expand All @@ -230,6 +231,7 @@ export class MongoDBCollection<T extends Document> implements Realm.Services.Mon
query: filter,
update,
upsert: options.upsert,
arrayFilters: options.arrayFilters,
});
}

Expand Down
4 changes: 4 additions & 0 deletions types/services.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ declare namespace Realm {
* When true, creates a new document if no document matches the query.
*/
readonly upsert?: boolean;
/**
* Array Filters
*/
readonly arrayFilters?: Filter[]
}

/**
Expand Down

0 comments on commit eb4ad71

Please sign in to comment.