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

473
Views
Objetos anidados en esquemas de mangosta

He visto muchas respuestas a esta pregunta aquí, pero todavía no las entiendo (tal vez porque usan ejemplos más "complejos")... Entonces, lo que estoy tratando de hacer es un esquema para un "Cliente", y tendrá dos campos que tendrán "subcampos" anidados, y otros que podrán repetirse. esto es lo que quiero decir:

 let customerModel = new Schema({ firstName: String, lastName: String, company: String, contactInfo: { tel: [Number], email: [String], address: { city: String, street: String, houseNumber: String } } });

tel y email pueden ser una matriz. y la dirección no se repetirá, pero tendrá algunos subcampos como puede ver.

¿Cómo puedo hacer que esto funcione?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

const mongoose = require("mongoose"); // Make connection // https://mongoosejs.com/docs/connections.html#error-handling mongoose.connect("mongodb://localhost:27017/test", { useNewUrlParser: true, useUnifiedTopology: true, }); // Define schema // https://mongoosejs.com/docs/models.html#compiling const AddressSchema = mongoose.Schema({ city: String, street: String, houseNumber: String, }); const ContactInfoSchema = mongoose.Schema({ tel: [Number], email: [String], address: { type: AddressSchema, required: true, }, }); const CustomerSchema = mongoose.Schema({ firstName: String, lastName: String, company: String, connectInfo: ContactInfoSchema, }); const CustomerModel = mongoose.model("Customer", CustomerSchema); // Create a record // https://mongoosejs.com/docs/models.html#constructing-documents const customer = new CustomerModel({ firstName: "Ashish", lastName: "Suthar", company: "BitOrbits", connectInfo: { tel: [8154080079, 6354492692], email: ["asissuthar@gmail.com", "contact.bitorbits@gmail.com"], }, }); // Insert customer object // https://mongoosejs.com/docs/api.html#model_Model-save customer.save((err, cust) => { if (err) return console.error(err); // This will print inserted record from database // console.log(cust); }); // Display any data from CustomerModel // https://mongoosejs.com/docs/api.html#model_Model.findOne CustomerModel.findOne({ firstName: "Ashish" }, (err, cust) => { if (err) return console.error(err); // To print stored data console.log(cust.connectInfo.tel[0]); // output 8154080079 }); // Update inner record // https://mongoosejs.com/docs/api.html#model_Model.update CustomerModel.updateOne( { firstName: "Ashish" }, { $set: { "connectInfo.tel.0": 8154099999, }, } );
over 4 years ago · Santiago Trujillo Report

0

// address model var addressModelSchema = new Schema({ city: String, street: String, houseNumber: String }) mongoose.model('address',addressModelSchema ,'address' ) // contactInfo model var contactInfoModelSchema = new Schema({ tel: [Number], email: [String], address: { type: mongoose.Schema.Type.ObjectId, ref: 'address' } }) mongoose.model('contactInfo ',contactInfoModelSchema ,'contactInfo ') // customer model var customerModelSchema = new Schema({ firstName: String, lastName: String, company: String, contactInfo: { type: mongoose.Schema.Type.ObjectId, ref: 'contactInfo' } }); mongoose.model('customer', customerModelSchema, 'customer') // add new address then contact info then the customer info // it is better to create model for each part.
over 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!