{
"_id": {
"$oid": "61d6552f4a40799193fc5904"
},
"attributes": [
{
"required": true,
"order": 1,
"attribute": {
"key": "title",
"label": "Title",
"dataType": "string",
"description": "just a test",
"searchable": true,
"inputControl": {
"controlType": "foo"
},
"searchControl": {}
}
}
],
"icon": "none.png",
}
based on the data above I want to delete an element from the attributes array based on the attribute.key property value.
Using MongoDB Playground I can accomplish this with…
db.getCollection('categories')
.updateOne(
{_id: ObjectId("61d6552f4a40799193fc5904")},
{$pull: { "attributes": { "attribute.key": "rest" } } }
);
but within my node typescript app the above violates the UpdateFilter<T>
interface that comes with the MongoDB drivers.
my base repository class that wraps the MongoDB driver lib…
import {
ClientSession, Filter, FindOptions, InsertOneOptions, InsertOneResult, OptionalUnlessRequiredId,
UpdateFilter, UpdateOptions, UpdateResult
} from 'mongodb';
public async mongoUpdateOne(filter: Filter<T>, update: UpdateFilter<T> | Partial<T>, options?: UpdateOptions): Promise<UpdateResult> {
return this.model.collection.updateOne(filter, update, options);
}
const filter: Filter<CategoryDocument> = { _id: new ObjectId(categoryId) };
const updateFilter = { $pull: { attributes: { 'attribute.key':'title' } } };
const updateResult: UpdateResult = await this.categoriesRepository.mongoUpdateOne(filter, updateFilter).catch((error) => Promise.reject(error));
Argument of type ‘{ $pull: { attributes: { ‘attribute.key’: string; }; }; }’ is not assignable to parameter of type ‘UpdateFilter | Partial’.
I can clear this up if I do not use the nested object 'attribute.key'
but thats where my unique key is.
Is there another way to write this update expression?