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

179
Views
How to create a field once in mongoose js

I am new to mongoose js so i wanted to create a contact feild only one time the user fills a form here is my contact model

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

    const ContactSchema = new Schema({
    fname: {
      type: String,
      required: true,
      default:"fname"
    },
    lname: {
      type: String,
      required: true,
      default:"lname"
    },email:{
      type:String, 
      required: true,
      default:""
    },
    mobile:{
        type:Number,
        required:true,
        default:""
    },
    title:{
       type:String,
       required:true,
       default:""
    }
  });
  
  module.exports = Contact = mongoose.model('Contact', ContactSchema);

   here is my user model

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

// Link Schema
const LinkSchema = require('./Link').schema;
const  ContactData=require('./conatct').schema;

// Create User Schema
const UserSchema = new Schema({
  username: {
    type: String,
    required: true,
    unique: true,
    lowercase:true,
    trim: true,
    
  },
  email: {
    type: String,
    required: true,
    unique: true,
    trim: true,
    lowercase:true
  },
  password: {
    type: String,
    required: true,
    trim: true
  },
  resetToken:{type:String},
  expireToken:{type:Date},
  date: {
    type: Date,
    default: Date.now
  },
  theme: {
    type: Number,
    default: 1
  },
  avatar: {
    type: String,
    default: 'uploads/default.png'
  },
  displayname:{
   type:String,
   default:"",
   trim:true
  },
  bio:{
    type: String,
      default: "",
  },
  title:{
    type: String,
      default: "",
  },
  links: {
    type: [LinkSchema]
  },
  contact:{
    type:[ContactData]
  }
});

module.exports = User = mongoose.model('user, UserSchema);

** suggest me some queries for creating contact details only once the user enters the data i have tried from user.contact.push({}) but it creates multiple object in contact array**

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If I get you, you want the contact field to be a single object not an array of objects then change

//change 

contact:{
    type:[ContactData]
 }
 
 //to 
 
 contact:ContactData
 
 
// when you want to add data  you just do something like

const user = new User({
name,etc...,
contact.mobile:user_mobile, etc..

})

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!