Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
MongoDB Problems while inserting nested data

I try to build a database for a giveaway bot using MongoDB. Once a new giveaway is created the bot runs the following code to write the new giveaway into the databank:

const {mongoose} = require("mongoose")
const mainSchema = require('../models/mainSchema')

module.exports = {
  async execute(interaction){

  await new mainSchema ({
    guild_id: 1234567890,
    giveaways: [{
      identifier: 34,
      destination: 987654321,                      
    }],                                                                        
  }).save()
}

Giveaways should be a nested document in the document with the server's information. My schemas for this looks like this:

const mongoose = require("mongoose");

const giveawaySchema = new mongoose.Schema({  
    identifier: String,
    destination: String,
    duration: String,
    price: String,
})

const mainSchema = new mongoose.Schema({
    guild_id: String,
    log_channel_id: String,
    twitterPreferncesType: String,
    twitterPreferncesFollowing: Boolean,
    giveaways: [giveawaySchema],
});

module.exports = mongoose.model("mainSchema", mainSchema);
module.exports = mongoose.model("giveawaySchema", giveawaySchema);

In my Mongo Atlas I have 2 collections mainSchema und giveawaySchema. If I run this code I only get an entry under 'giveawaySchema' consisting of this:

_id: ObjectId(62b4f6374894fb3e7826ca72)
__v:0

I don't get any data in the mainSchema collection. Does anyone know what is the error? Thank you for your help in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

in your first code you don't have to put mongoose module between brackets (i don't know if it's okay) and you are importing giveawaySchema not mainSchema because you are overwriting it you should do this

// module.exports = mongoose.model("mainSchema", mainSchema);
// module.exports = mongoose.model("giveawaySchema", giveawaySchema);
// Wrong you are overwriting on the exports 

// do this instead 
const MainSchema = mongoose.model("mainSchema", mainSchema);
const GiveawaySchema = mongoose.model("giveawaySchema", giveawaySchema);
module.exports = {MainSchema,GiveawaySchema}

// and import it like this 
// const { MainSchema,GiveawaySchema} = require('./<pathOfTheFile>')

and if you still have problem just tell me Good luck ...

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda