DEV Community

Cover image for Mongoose Schema type required, lowercase, uppercase, trim, minLength,maxLength#challange 2
swapnanilWebDeveloper
swapnanilWebDeveloper

Posted on

Mongoose Schema type required, lowercase, uppercase, trim, minLength,maxLength#challange 2

I have already uploaded another challange1 . Go through that code briefly and then come to this example.
If you already have studied that previous challange1 now you are ready for this challange2.
follow my github account for more of these mongoose concepts

click to follow

const mongoose = require('mongoose');

main().catch(err => console.log(err));

async function main() {
    await mongoose.connect('mongodb://127.0.0.1:27017/PersonData');

    const personSchema = new mongoose.Schema({
        name: {
            first: { type: String, required: true, lowercase: true, trim: true },
            last: { type: String, required: true, uppercase: true, trim: true }
        },

        username: {
            type: String,
            required: true,
            lowercase: true,
            trim: true,
            minLength: 6,
            maxLength: 25,
        },

        email: {
            type: String,
            required: true,
            uppercase: true,
            trim: true,
            minLength: 6,
            maxLength: 35,
        },

        living: Boolean,
        updated: {
            type: Date,
            default: Date.now,
        },

        age: {
            type: Number,
            min: [18, 'You are too young to be in profession'],
            max: [65, 'Now you can retire...'],
        },

        occupation: {
            type: String,
            required: true,
        }
    });

    // compile our model
    const Person = mongoose.model('Person', personSchema);

    // create a document
    await Person.insertMany([
        {
            name: {
                first: 'Mayuk',
                last: 'Mukherjee',
            },
            username: "Mayuk_ProgrammER_USER",
            email: "Mayuk@GMAIL.com",
            living : true,
            updated: new Date,
            age : 19,
            occupation: "Software Developer",
        },
        {
            name: {
                first: 'Suryendu',
                last: 'Sarkar',
            },
            username: "Suryendu_MediCAL_USER",
            email: "Suryendu@Gmail.com",
            living : true,
            updated: new Date,
            age : 54,
            occupation: "MBBS Doctor",
        },
        {
            name: {
                first: 'Aninda',
                last: 'Mukherjee',
            },
            username: "Aninda_Mathematician_USER",
            email: "Aninda@Gmail.com",
            living : false,
            updated: new Date,
            age : 55,
            occupation: "MBBS Doctor",
        },
        {
            name: {
                first: 'Snlap',
                last: 'Gadai',
            },
            username: "Sanlap_Physist_USER",
            email: "Sanlap@Gmail.com",
            living : false,
            updated: new Date,
            age : 48,
            occupation: "Physics Masters",
        },
        {
            name: {
                first: 'Souvik',
                last: 'Mondal',
            },
            username: "Souvik_UnEmployeed_USER",
            email: "Souvik@Gmail.com",
            living : true,
            updated: new Date,
            age : 48,
            occupation: "Tier 3 Engineer",
        },
    ]);

    const allPerson = await Person.find({});
    console.log(allPerson);

    for (var i = 0; i < allPerson.length; i++){
        console.log("first Name = " + allPerson[i].name.first + ", last Name = " + allPerson[i].name.last);
        console.log("userName = "+allPerson[i].username);
        console.log("email = "+allPerson[i].email);
        console.log("living = "+allPerson[i].updated);
        console.log("updated = "+allPerson[i].age);
        console.log("occupation = "+allPerson[i].occupation);
    }

}

Enter fullscreen mode Exit fullscreen mode

Here is the output :


PS C:\Users\USER\Downloads\mongoExpress\SchemaModel_NestedData> node src/nested_3.js
[
  {
    name: { first: 'Mayuk', last: 'Mukherjee' },
    _id: new ObjectId('65c52a310f778a59bff71c82'),
    email: 'Mayuk@gmail.com',
    address: { state: 'Alabama', district: 'Denver', city: 'Ohio' },
    facebook: { votes: 2045, favs: 42 },
    nested: { stuff: 'i am stuff number 1' },
    age: 34,
    living: true,
    hidden: false,
    sport_array: [
      'football',   16,
      'cricket',    22,
      'badminton',  25,
      'hockey',     29,
      'basketball'
    ],
    singer_arrayofString: [
      'Taylor Swift',
      'Justin Bieber',
      'Bruno Mars',
      'Zayn Mallik',
      'Ed Sheeran'
    ],
    phoneModel_ofNumber: [
      7, 11, 2, 4,
      6, 16, 9
    ],
    updated: 2024-02-08T19:23:29.403Z,
    comments: [ [Object], [Object], [Object] ],
    __v: 0
  },
  {
    name: { first: 'mayuk', last: 'MUKHERJEE' },
    _id: new ObjectId('65c52ee0051d28a32bc84195'),
    username: 'mayuk_programmer_user',
    email: 'MAYUK@GMAIL.COM',
    living: true,
    updated: 2024-02-08T19:43:28.871Z,
    age: 19,
    occupation: 'Software Developer',
    __v: 0
  },
  {
    name: { first: 'suryendu', last: 'SARKAR' },
    _id: new ObjectId('65c52ee0051d28a32bc84196'),
    username: 'suryendu_medical_user',
    email: 'SURYENDU@GMAIL.COM',
    living: true,
    updated: 2024-02-08T19:43:28.871Z,
    age: 54,
    occupation: 'MBBS Doctor',
    __v: 0
  },
  {
    name: { first: 'aninda', last: 'MUKHERJEE' },
    _id: new ObjectId('65c52ee0051d28a32bc84197'),
    username: 'aninda_mathematician_user',
    email: 'ANINDA@GMAIL.COM',
    living: false,
    updated: 2024-02-08T19:43:28.871Z,
    age: 55,
    occupation: 'MBBS Doctor',
    __v: 0
  },
  {
    name: { first: 'snlap', last: 'GADAI' },
    _id: new ObjectId('65c52ee0051d28a32bc84198'),
    username: 'sanlap_physist_user',
    email: 'SANLAP@GMAIL.COM',
    living: false,
    updated: 2024-02-08T19:43:28.871Z,
    age: 48,
    occupation: 'Physics Masters',
    __v: 0
  },
  {
    name: { first: 'souvik', last: 'MONDAL' },
    _id: new ObjectId('65c52ee0051d28a32bc84199'),
    username: 'souvik_unemployeed_user',
    email: 'SOUVIK@GMAIL.COM',
    living: true,
    updated: 2024-02-08T19:43:28.871Z,
    age: 48,
    occupation: 'Tier 3 Engineer',
    __v: 0
  }
]
first Name = Mayuk, last Name = Mukherjee
userName = undefined
email = Mayuk@gmail.com
living = Fri Feb 09 2024 00:53:29 GMT+0530 (India Standard Time)
updated = 34
occupation = undefined
first Name = mayuk, last Name = MUKHERJEE
userName = mayuk_programmer_user
email = MAYUK@GMAIL.COM
living = Fri Feb 09 2024 01:13:28 GMT+0530 (India Standard Time)
updated = 19
occupation = Software Developer
first Name = suryendu, last Name = SARKAR
userName = suryendu_medical_user
email = SURYENDU@GMAIL.COM
living = Fri Feb 09 2024 01:13:28 GMT+0530 (India Standard Time)
updated = 54
occupation = MBBS Doctor
first Name = aninda, last Name = MUKHERJEE
userName = aninda_mathematician_user
email = ANINDA@GMAIL.COM
living = Fri Feb 09 2024 01:13:28 GMT+0530 (India Standard Time)
updated = 55
occupation = MBBS Doctor
first Name = snlap, last Name = GADAI
userName = sanlap_physist_user
email = SANLAP@GMAIL.COM
living = Fri Feb 09 2024 01:13:28 GMT+0530 (India Standard Time)
updated = 48
occupation = Physics Masters
first Name = souvik, last Name = MONDAL
userName = souvik_unemployeed_user
email = SOUVIK@GMAIL.COM
living = Fri Feb 09 2024 01:13:28 GMT+0530 (India Standard Time)
updated = 48
occupation = Tier 3 Engineer

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
swapnanilwebdeveloper profile image
swapnanilWebDeveloper

Any buddy find some difficulties to understand this program comment here at this page and let me know how to help you