Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Getting item in an array with matching id mongoose

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: []}
        
    })
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!