Mongoose find to list rows that has a field with a list of ids that include an ID

Schema:

const BookSchema = new Schema(
  {
    ...
    authors: [{ type: Schema.ObjectId, ref: 'Author' }],
  }
);

I want to find books that include an author ID. I tried the following:

  const books = await Book.find({
    some_field: someValue,
    $or: [{ authors: author.id }, {...some other condition} ],
  });

What am I doing wrong?

1 answer

  • answered 2019-10-08 03:20 narayan maity

    There is one word you missed using this block of code hope you will get your author.....

    const BookSchema = new Schema(
      {
        ...
        authors:{ type: Schema.Types.ObjectId, ref: 'Author' },
      }
    );
    

    Use this....