This is a sample data entry.
{
_id: ObjectId('63e501cc2054071132171098'),
name: 'Ricky',
discriminator: 7706,
registerTime: ISODate('2023-02-09T14:23:08.159Z'),
friends: {
'63e502f4e196ec7c04c4351e': {
friendId: ObjectId('63e502f4e196ec7c04c4351e'),
friendshipStatus: null,
privateChannelId: ObjectId('63e66dd85eb6c3cc248ecc95'),
active: true
}
}
}
I need to modify the "friends.63e502f4e196ec7c04c4351e.friendshipStatus" property from null to 1 without replacing the entire subdocument (I want to retain the "friendId", "privateChannelId", and "active" properties)
so far I have attempted
const friendUpdateResult = await collections.users!.updateOne(
{
_id: targetFriend._id,
},
{
$set: {
[`friends.${currentUser._id.toString()}.friendshipStatus`]: null,
},
}
);
however, the use of "$set" is causing an error
Type '{ [x: string]: null; }' is not assignable to type 'Readonly<{ [x: `friends.${string}`]: unknown; [x: `friends.${string}.friendId`]: Friend | undefined; [x: `friends.${string}.friendshipStatus`]: Friend | undefined; [x: `friends.${string}.privateChannelId`]: Friend | undefined; [x: `friends.${string}.active`]: Friend | undefined;.
'string' and '`friends.${string}.friendId`' index signatures are incompatible.
Type 'null' is not assignable to type 'Friend | undefined'.
the interface for this collection is as follows:
interface User {
name: string;
discriminator: number;
registerTime: Date;
friends: Record<string, Friend>;
}
interface Friend {
friendId: ObjectId;
friendshipStatus?: FriendshipEnum | null;
privateChannelId?: ObjectId;
active?: boolean;
}