You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: MongoDB/populate-method-mongoose-referencing-other-model.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,7 @@ function getUserWithPosts(username) {
78
78
}
79
79
```
80
80
81
-
.populate() needs a query to attach itself to, so we are using User.findOne() to find a user who matches the username we provide in the argument. This returns our user document. This is when .populate() takes over.
81
+
`.populate()` needs a query to attach itself to, so we are using User.findOne() to find a user who matches the username we provide in the argument. This returns our user document. This is when .populate() takes over.
82
82
83
83
#### You’ll notice I am providing ‘posts’ to our .populate(). By providing the ‘posts’ argument, we’ve told .populate() what property in our user document we want it to work with. Calling .exec() just executes something once .populate() has done it’s thing. The log prints this:
### Inserting new attribute to an existing document in MongoDB
2
+
3
+
Say first I have the below user collection with only one document, and I can see that by running `db.users.find().pretty()` . Now I want to add another attribute or property to this document named 'port' and given this attribute is of `Schema.Types.ObjectId` type in `user` collection, I have to add the corresponding `port's`\_id field to it as its value. So lets say that field is - "5bd983e4a18ceecf4bba3c48"
Bcrypt allows us to choose the value of saltRounds, which gives us control over the cost of processing the data. The higher this number is, the longer it takes for the machine to calculate the hash associated with the password. It is important when choosing this value, to select a number high enough that someone who tries to find the password for a user by brute force, requires so much time to generate all the possible hash of passwords that does not compensate him. And on the other hand, it must be small enough so as not to end the user’s patience when registering and logging in (this patience is not usually very high). By default, the saltRounds value is 10.
42
43
43
44
### One example of the first step of generating the salt and hashing - [https://github.com/rohan-paul/Tiny-Twitter-Clone/blob/master/models/user.js](https://github.com/rohan-paul/Tiny-Twitter-Clone/blob/master/models/user.js)
44
45
45
46
```js
46
-
UserSchema.pre('save', function(next) {
47
+
UserSchema.pre("save", function(next) {
47
48
let user =this; // This is how I access UserSchema object
48
49
49
50
// I shall only hash the password if it has been modified (or is new). So, in below line I make sure if there was already a password and isModified in not true, then move-on with next()
Explanation of ``UserSchmea.pre`` in above - Its the middleware - also known as “pre” and “post” hooks that tie particular functions to particular lifecycle and query events. This middleware is defined on the schema level and can modify the query or the document itself as it is executed. Middleware is invoked with two arguments: the event trigger (as a string) and the callback function that is triggered for that particular event. The callback itself takes in an argument of a function, which we typically call next , and when invoked — advances the document/query to the next awaiting middleware.
67
+
Explanation of `UserSchmea.pre` in above - Its the middleware - also known as “pre” and “post” hooks that tie particular functions to particular lifecycle and query events. This middleware is defined on the schema level and can modify the query or the document itself as it is executed. Middleware is invoked with two arguments: the event trigger (as a string) and the callback function that is triggered for that particular event. The callback itself takes in an argument of a function, which we typically call next , and when invoked — advances the document/query to the next awaiting middleware.
67
68
So what the below function does is - before (i.e. pre) user saves the normal text password into the database, making sure it encrypts it first
68
69
69
-
70
70
### Example from my [dev-book app - https://github.com/rohan-paul/Developer-Profile-App/blob/master/routes/api/users.js](https://github.com/rohan-paul/Developer-Profile-App/blob/master/routes/api/users.js)
71
+
71
72
### First hashing and then save the hashed password into mongo
72
73
73
74
```js
74
75
// and then get other details of the new user from the post request
### Next step before signing-in a new user, it compares the password with that saved in the database
95
96
96
97
Example [https://github.com/rohan-paul/Developer-Profile-App/blob/master/routes/api/users.js](https://github.com/rohan-paul/Developer-Profile-App/blob/master/routes/api/users.js)
97
98
98
99
```js
99
-
router.post('/login', (req, res) => {
100
-
101
-
/* I pass the data to the validateRegisterInput() function. The data (i.e. req.body) includes
100
+
router.post("/login", (req, res) => {
101
+
/* I pass the data to the validateRegisterInput() function. The data (i.e. req.body) includes
102
102
all the information that the user puts in while registering.
103
103
And get the function's return values assigned to const { errors, isValid }. */
Just put the rounds (which is the salt length to generate, i.e. the function wheere I am hashing the plain-text password )
201
-
202
-
``bcrypt.hashSync(plainTextPassword, 10)`` So the number 10 is the rounds in the above online tool
203
-
204
-
After hashing a plaintext password, for checking I will just put the hashed password from the mongo database - i.e. after running terminal command something like ``db.users.find()`` which will give all the users saved in the mongo database.
205
-
206
-
So an example is this hashed password - ``$2a$10$m0mq4PYOOvm74Gukml4FN.T0Ntobhzi42T6b5v1WIsJ5aZkVzJz3a`` And then put the round as 10 and I will get ``123`` which was my plaintext password in this case.
207
-
208
-
204
+
Just put the rounds (which is the salt length to generate, i.e. the function where I am hashing the plain-text password )
209
205
206
+
`bcrypt.hashSync(plainTextPassword, 10)` So the number 10 is the rounds in the above online tool
210
207
208
+
After hashing a plaintext password, for checking I will just put the hashed password from the mongo database - i.e. after running terminal command something like `db.users.find()` which will give all the users saved in the mongo database.
211
209
210
+
So an example is this hashed password - `$2a$10$m0mq4PYOOvm74Gukml4FN.T0Ntobhzi42T6b5v1WIsJ5aZkVzJz3a` And then put the round as 10 and I will get `123` which was my plaintext password in this case.
0 commit comments