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

103
Views
Array of {key: value} in mongoose schema

I have a comments field in my interface and schema as follows

export interface Blog extends Document {
    ...
    comments: { user: ObjectId; comment: string }[]
    ...
}


const blogSchema = new Schema<Blog>({
    ...
    comments: [
        {
            user: {
                type: ObjectId
            },
            comment: {
                type: String
            }
        }
    ],
    ...
})

However, schema throws this error 'ObjectId' only refers to a type but is being used as a value here.. I know that schema is written kind of in a weird way, but I'm confused with how to improve.

What I want in database is something like

[
    {
        user: "Vivid",
        comment: "Vivid's comment"
    },
    {
        user: "David",
        comment: "David's comment"
    }
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe you need to change ObjectId to

mongoose.Schema.Types.ObjectId

Either that, or you could do something like:

const mongoose = require('mongoose');
// ... other imports
const { ObjectId } = mongoose.Schema.Types;
// ... now use in code as `ObjectId`
about 4 years ago · Juan Pablo Isaza 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!