Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda