Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!