Home > database >  Mongose findOne in an object
Mongose findOne in an object

Time:12-13

const mongoose = require("mongoose");

const schema = new mongoose.Schema({
  integrations: {
    discord: {
      guild: {
        type: String,
        maxlength: [20, "Guild ID cannot be longer than 20 characters"],
        required: true,
      },
    },
  },
});

module.exports = mongoose.model("Hub", schema);

That is my mongoose schema. How do I get a Hub by its guild in integrations.discord.guild when using .findOne

CodePudding user response:

For Query on Nested Field

Hub.findOne({'integrations.discord.guild': '----'})

CodePudding user response:

This won't be possible using single query because MongoDB doesn't support joins.

You need to break this query into two separate queries.

  • Related