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

227
Visualizações
Is there way to add array of objectid and other field in mongodb schema?

I wanted to create a schema , with multiple object id with each object id containing a Number.

Like for this data,

[ "user": [["collection1",1], ["collection2", 3], ["collection4": 4]

where user is a user object id, and collection1, collection2 .... are collection object id, and other are just number.

const derivedSchema = new mongoose.Schema(
  {
    user: {type: ObjectId, ref: "User"},
    collections: {type: [ObjectId], ref="collection"}],
);

I tried to change like put [ObjectId, Number] if it would look like my data

and tried to save as like this

let collected = new derivedSchema({
        "user": uid,
        "collections": [cid, 12],
      })
      collected.save()

where cid is collectionid, uid is userid , 12 number, i passed through in for loop.

But it is not working. Is there any way to solve this ? Thank you

At end result i should show like

[
   "user1": { ["col1",1], ["col2", 5]},
   "user2": { ["col1",55], ["col2", 35]},
]
  

Any idea would be really helpful?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

var mongoose = require('mongoose');
var Schema=mongoose.Schema;

const derivedSchema = new mongoose.Schema(
  {
    user: {
           type: Schema.Types.ObjectId,
           ref: "User"
    },
    collections:{
        type:[Schema.Types.ObjectId],
        ref:"collection"
    }
);
about 4 years ago · Santiago Trujillo 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