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

200
Views
MongoDB/GraphQL: Model.find() to get posts that userId is in likes

in my resolvers I have a method to find user likes with

async function userBookmarks(args, context) {
const user = checkAuth(context);
const posts = await Post.find({likes: {userId: user.id}})
return posts; }

But GraphQL returns an empty array.

For reference, the Post model is

likes: [
{
  userId: String,
  createdAt: String
}],
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I came across a similar problem and fixed it by defining the MongoDB Collection name in the bottom of my MongoDB Schema.

const UserSchema = new mongoose.Schema({
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    location: {
        type: String,
        required: false
    }
}, { collection : 'Users' });

EDIT: Use $elemMatch to query several fields.

const posts = await User.find({likes: {$elemMatch: {userId: user.id}}})
about 4 years ago · Juan Pablo Isaza Report
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!