I have a db with some objects that have embedded array of cards.
I want to get a card with matching id from a user that matches the Id that I am looking for. so far I am only able to return the wrong card.
async getCard(id: string, cardId: string) {
if (!id) throw new CardNotFoundError(id);
let query = {'user.id': id, 'cards.id': cardId};
const card = (await this.byQuery(query)).cards[0];
if (card) return card;
throw new CardNotFoundError("" || id);
}
my schema
const CardUserSchema = new Schema(
{
id: {
...trimmedString,
required: true,
index: true,
unique: true
},
phoneNumber: { ...trimmedString, required: true, unique: true },
email: { ...trimmedString, required: false },
}
const CardSchema = new Schema({
id: {
...trimmedString,
required: true,
index: true,
unique: true
},
})
const CardMainSchema = SchemaFactory(
{
id: {
...trimmedString,
required: true,
index: true,
unique: true
}
user: { type: CardUserSchema, required: true },
cards: {type: [CardSchema], default: []}
})