0

I'm using mongoose to connect to MongoDB and I have a doubt about how can I make a query between two related collections

I have these Schemas.

const users = new mongoose.Schema({
  name: String,
  lastname: String,
  age: Number,
  comments: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Comments',
    }
  ],
}, { timestamp: true, strict: false });

const comments = new mongoose.Schema({
  message: {
    type: String,
  },
  description: {
    type: String,
  },
  candidates: Number,
  
}, { timestamp: true, strict: false });

well, the idea is get all users that contains comments with candidates value > 100 Is this possible to do just one query that will return the users that have comments with that condition?

TIA !

  • use [$lookup](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/)? – 1sina1 Apr 14 '22 at 12:40
  • Does this answer your question? [How do I perform the SQL Join equivalent in MongoDB?](https://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb) – ray Apr 14 '22 at 13:00

0 Answers0