0

I don't know how to make a list of documents with mongoose (Node JS), someone know's how to make that?

new mongoose.Schema({
   first_name: String,
   last_name: String,
   companies: [
      {
         name_company:String,
         post:String,
         time_in_post:String,
      }     

   ] 
});

I need to insert many documents with companies schema at different times but I don't know how to make that with mongoose.

Can someone help me?

1
  • What is wrong with what you have implemented? That seems correct. I would add that you could make 2 different schema's and have them reference each other so that you don't duplicate unnecessarily.. i.e. User schema and Company schema.
    – dillon
    Apr 17, 2020 at 0:17

2 Answers 2

1

Seems like splitting this into different schemas might be a good idea. Especially if other people will be apart of the same company.

For instance, maybe you could try this:


const CompanySchema = new mongoose.Schema({
    name: String,
    // ... other company specific attributes
});

const Company = mongoose.model("Company", CompanySchema);

const UserSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    companies: [
        {
            company: { type: mongoose.Schema.Types.ObjectId, ref: 'Company' },
            post: String,
            timeInPost: String,
        }
    ]
});

const User = mongoose.model("User", UserSchema);


And the interaction with the models could look like this:

(async () => {
    try {
        const company1 = await Company.create({ name: "Company 1" });
        const company2 = await Company.create({ name: "Company 2" });

        const user1 = await User.create({
            firstName: "first",
            lastName: "last",
            companies: [
                {
                    company: company1._id,
                    post: "....",
                    timeInPost: "....",
                },
                {
                    company: company2._id,
                    post: "....",
                    timeInPost: "....",
                },
            ],
        });

        const users = await User.find({}).populate("companies.company"); // in order to populate the company with the document from the company collection

        // users with companies info
    } catch (error) {
        console.log(error);
    }
})();

Referenced from the mongoose documentation: https://alexanderzeitler.com/articles/mongoose-referencing-schema-in-properties-and-arrays/

0

You could use this approach, with this way you avoid data duplication:

const companySchema = new mongoose.Schema({
  name_company: String,
  post: String,
  time_in_post: String
});

new mongoose.Schema({
  firstName: String,
  lastName: String,
  companies: [companySchema] // Array of subdocuments
});

See full documentation here: https://mongoosejs.com/docs/guide.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.