{"id":15190600,"url":"https://github.com/surendravidiyala/mongoose-population","last_synced_at":"2025-02-28T06:19:43.473Z","repository":{"id":139643315,"uuid":"93791548","full_name":"SurendraVidiyala/mongoose-population","owner":"SurendraVidiyala","description":"Mongoose population to populate information in a document from a referenced document. ","archived":false,"fork":false,"pushed_at":"2017-06-08T22:01:21.000Z","size":4069,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-11T02:17:46.512Z","etag":null,"topics":["express","express-middleware","jsonwebtoken","mongo","mongodb","mongoose","mongoose-model","mongoose-population","mongoose-schema","nodejs","nodejs-modules","population","restful-api"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SurendraVidiyala.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-08T21:01:35.000Z","updated_at":"2017-06-08T22:04:57.000Z","dependencies_parsed_at":"2023-07-23T23:01:34.043Z","dependency_job_id":null,"html_url":"https://github.com/SurendraVidiyala/mongoose-population","commit_stats":{"total_commits":8,"total_committers":2,"mean_commits":4.0,"dds":0.125,"last_synced_commit":"22e7472d85244d66ae3a66959008dddd96afd683"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SurendraVidiyala%2Fmongoose-population","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SurendraVidiyala%2Fmongoose-population/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SurendraVidiyala%2Fmongoose-population/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SurendraVidiyala%2Fmongoose-population/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SurendraVidiyala","download_url":"https://codeload.github.com/SurendraVidiyala/mongoose-population/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241110759,"owners_count":19911392,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","express-middleware","jsonwebtoken","mongo","mongodb","mongoose","mongoose-model","mongoose-population","mongoose-schema","nodejs","nodejs-modules","population","restful-api"],"created_at":"2024-09-27T20:42:59.248Z","updated_at":"2025-02-28T06:19:43.451Z","avatar_url":"https://github.com/SurendraVidiyala.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-population\n* Referencing one document from another in MongoDBcan be acheived by using Mongoose population   to populate information in a document from a referenced document. \n  - Use Mongoose population to cross-reference users within a comment\n  - Populate the comments with the users’ information upon querying\n\n````\nvar User = new Schema({\n    username: String,\n    password: String,\n    firstname: {\n      type: String,\n        default: ''\n    },\n    lastname: {\n      type: String,\n        default: ''\n    },\n    admin:   {\n        type: Boolean,\n        default: false\n    }\n});\nUser.methods.getName = function() {\n    return (this.firstname + ' ' + this.lastname);\n};\n````\n- In the models do the changes to utilize the population\n* Comment schema\n````\nvar commentSchema = new Schema({\n    rating:  {\n        type: Number,\n        min: 1,\n        max: 5,\n        required: true\n    },\n    comment:  {\n        type: String,\n        required: true\n    },\n    postedBy: {\n        type: mongoose.Schema.Types.ObjectId,\n        ref: 'User'\n    }\n}, {\n    timestamps: true\n});\n````\n# Updating users.js route\n- Open users.js and update the register function as follows:\n````\nrouter.post('/register', function(req, res) {\n    User.register(new User({ username : req.body.username }),\n        req.body.password, function(err, user) {\n        if (err) {\n            return res.status(500).json({err: err});\n        }\n                if(req.body.firstname) {\n            user.firstname = req.body.firstname;\n        }\n        if(req.body.lastname) {\n            user.lastname = req.body.lastname;\n        }\n                user.save(function(err,user) {\n            passport.authenticate('local')(req, res, function () {\n                return res.status(200).json({status: 'Registration Successful!'}\n                  );\n            });\n        });\n    });\n});\n````\nSave all the changes and start the server and test.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurendravidiyala%2Fmongoose-population","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsurendravidiyala%2Fmongoose-population","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurendravidiyala%2Fmongoose-population/lists"}