Skip to content

Commit 2d5d654

Browse files
committed
small notes in mongo
1 parent b9197d7 commit 2d5d654

10 files changed

+200
-110
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### A challenging case or assignment or technical problem you have solved
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
### Delete a single document from collection
2+
3+
Say I have the following in `users` collections (i.e. when I run `db.users.find().pretty()` I get the below)
4+
5+
```
6+
{
7+
"_id" : ObjectId("5bd982dea18ceecf4bba3c47"),
8+
"username" : "test-user-2",
9+
"post" : "123456"
10+
}
11+
{
12+
"_id" : ObjectId("5bd983e4a18ceecf4bba3c48"),
13+
"username" : "p@gmail.com",
14+
"password" : "$2a$10$HyXCD5.4U/0CvZHq9SDQ0uxD12BQ46yVAHu18lRRVEQZB3uyHXgy.",
15+
"__v" : 0
16+
}
17+
```
18+
19+
And to delete just the top-most document with id of "5bd982dea18ceecf4bba3c47" run the below command
20+
21+
```
22+
try {
23+
db.users.deleteOne( { "_id" : ObjectId("5bd982dea18ceecf4bba3c47") } );
24+
} catch (e) {
25+
print(e);
26+
}
27+
```
28+
29+
[https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/](https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/)
30+
31+
And now running `db.users.find().pretty()` will give me just
32+
33+
```
34+
{
35+
"_id" : ObjectId("5bd983e4a18ceecf4bba3c48"),
36+
"username" : "p@gmail.com",
37+
"password" : "$2a$10$HyXCD5.4U/0CvZHq9SDQ0uxD12BQ46yVAHu18lRRVEQZB3uyHXgy.",
38+
"__v" : 0
39+
}
40+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### To remove/delete all data under field name “messages” within an existing database - $ db.messages.remove({})
2+
3+
1> To remove/delete all data under field name (or collection name ) “messages” within any existing databae -
4+
5+
`$ db.messages.remove({})`
6+
7+
But the above will NOT completely remove this collection. It will only empty this collection of all the documents in it.
8+
9+
### TO DROP THE COLLECTION COMPLETELY
10+
11+
`$ db.mesages.drop()`
12+
13+
drop() method will return true, if the selected collection is dropped successfully, otherwise it will return false.
14+
15+
To REMOVE / DELETE the whole database
16+
17+
`$ show dbs`
18+
19+
I will get the below in my terminal
20+
21+
```
22+
employee 0.000GB
23+
mern-book-library 0.000GB
24+
mern-crud 0.000GB
25+
```
26+
27+
then first move into that database before deleting
28+
29+
`$ use employee`
30+
31+
`$ db.dropDatabase()`

MongoDB/populate-method-mongoose.md renamed to MongoDB/populate-method-mongoose-referencing-other-model.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function getUserWithPosts(username) {
7878
}
7979
```
8080

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.
8282

8383
#### 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:
8484

MongoDB/quick-mongodb-snippets.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### 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"
4+
5+
```
6+
{
7+
"_id" : ObjectId("5bd983e4a18ceecf4bba3c48"),
8+
"username" : "p@gmail.com",
9+
"password" : "$2a$10$HyXCD5.4U/0CvZHq9SDQ0uxD12BQ46yVAHu18lRRVEQZB3uyHXgy.",
10+
"__v" : 0
11+
}
12+
```
13+
14+
So the command to add `port` value is
15+
16+
```
17+
db.users.update({'_id' : ObjectId("5bd983e4a18ceecf4bba3c48")},
18+
{'$set' : {'port' : "5bdaadc9ebae106aaa8ca247" }})
19+
```

Node-Express/bcrypt-How-it-works.md renamed to Node-Express/bcrypt-How-it-works-de-hashing.md

+108-109
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,30 @@ So from [official doc](https://github.com/dcodeIO/bcrypt.js#usage---async) the b
2828

2929
```js
3030
var salt = bcrypt.genSaltSync(10);
31-
var hash = bcrypt.hashSync("B4c0/\/", salt);
31+
var hash = bcrypt.hashSync("B4c0//", salt);
3232

3333
// And to hash a password the ES6 Async way is combine them both without blocking any other operation
3434

35-
bcrypt.genSalt(10, function(err, salt) {
36-
bcrypt.hash("B4c0/\/", salt, function(err, hash) {
37-
// Store hash in your password DB.
38-
});
35+
bcrypt.genSalt(10, function(err, salt) {
36+
bcrypt.hash("B4c0//", salt, function(err, hash) {
37+
// Store hash in your password DB.
38+
});
3939
});
4040
```
41+
4142
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.
4243

4344
### 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)
4445

4546
```js
46-
UserSchema.pre('save', function(next) {
47+
UserSchema.pre("save", function(next) {
4748
let user = this; // This is how I access UserSchema object
4849

4950
// 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()
50-
if(!user.isModified('password')) return next();
51+
if (!user.isModified("password")) return next();
5152

5253
// and for new password
53-
if(user.password) {
54+
if (user.password) {
5455
bcrypt.genSalt(10, function(err, salt) {
5556
if (err) return next(err);
5657
bcrypt.hash(user.password, salt, null, function(err, hash) {
@@ -63,87 +64,84 @@ UserSchema.pre('save', function(next) {
6364
});
6465
```
6566

66-
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.
6768
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
6869

69-
7070
### 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+
7172
### First hashing and then save the hashed password into mongo
7273

7374
```js
7475
// and then get other details of the new user from the post request
75-
const newUser = new User({
76-
name: req.body.name,
77-
email: req.body.email,
78-
avatar,
79-
password: req.body.password
80-
});
81-
82-
bcrypt.genSalt(10, (err, salt) => {
83-
bcrypt.hash(newUser.password, salt, (err, hash) => {
84-
if (err) throw err;
85-
newUser.password = hash;
86-
newUser
87-
.save()
88-
.then(user => res.json(user))
89-
.catch(user => console.log(err))
90-
});
91-
});
76+
const newUser = new User({
77+
name: req.body.name,
78+
email: req.body.email,
79+
avatar,
80+
password: req.body.password
81+
});
82+
83+
bcrypt.genSalt(10, (err, salt) => {
84+
bcrypt.hash(newUser.password, salt, (err, hash) => {
85+
if (err) throw err;
86+
newUser.password = hash;
87+
newUser
88+
.save()
89+
.then(user => res.json(user))
90+
.catch(user => console.log(err));
91+
});
92+
});
9293
```
9394

9495
### Next step before signing-in a new user, it compares the password with that saved in the database
9596

9697
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)
9798

9899
```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
102102
all the information that the user puts in while registering.
103103
And get the function's return values assigned to const { errors, isValid }. */
104-
const { errors, isValid } = validateLoginInput(req.body);
105-
// Check Validation
106-
if(!isValid) {
107-
return res.status(400).json(errors);
108-
}
104+
const { errors, isValid } = validateLoginInput(req.body);
105+
// Check Validation
106+
if (!isValid) {
107+
return res.status(400).json(errors);
108+
}
109109

110-
const email = req.body.email;
111-
const password = req.body.password;
110+
const email = req.body.email;
111+
const password = req.body.password;
112112

113-
//Check if the user email exists at all in database
114-
User.findOne({ email }).then(user => {
115-
if (!user) {
116-
errors.email = 'User not found';
117-
return res.status(404).json({email: 'User not found'});
118-
}
113+
//Check if the user email exists at all in database
114+
User.findOne({ email }).then(user => {
115+
if (!user) {
116+
errors.email = "User not found";
117+
return res.status(404).json({ email: "User not found" });
118+
}
119119

120-
// Compare password hash saved in database with the password provided in the req.body
121-
bcrypt.compare(password, user.password).then(isMatch => {
122-
if(isMatch) {
123-
124-
// if User is matched, then create JWT Payload
125-
const payload = { id: user.id, name: user.name, avatar: user.avatar };
126-
127-
// signed JWT token to be sent to server
128-
jwt.sign(
129-
payload,
130-
keys.secretOrKey,
131-
{ expiresIn: 3600 },
132-
(err, token) => {
133-
res.json({
134-
success: true,
135-
token: 'Bearer ' + token
136-
});
137-
}
138-
);
139-
} else {
140-
errors.password = 'Password Incorrect';
141-
return res.status(400).json(errors);
142-
}
143-
});
120+
// Compare password hash saved in database with the password provided in the req.body
121+
bcrypt.compare(password, user.password).then(isMatch => {
122+
if (isMatch) {
123+
// if User is matched, then create JWT Payload
124+
const payload = { id: user.id, name: user.name, avatar: user.avatar };
125+
126+
// signed JWT token to be sent to server
127+
jwt.sign(
128+
payload,
129+
keys.secretOrKey,
130+
{ expiresIn: 3600 },
131+
(err, token) => {
132+
res.json({
133+
success: true,
134+
token: "Bearer " + token
135+
});
136+
}
137+
);
138+
} else {
139+
errors.password = "Password Incorrect";
140+
return res.status(400).json(errors);
141+
}
144142
});
143+
});
145144
});
146-
147145
```
148146

149147
### Authentication at the time of signing in a user
@@ -157,55 +155,56 @@ Looking at the [source code of bcrypt.compare](https://github.com/dcodeIO/bcrypt
157155

158156
```js
159157
bcrypt.compare = function(s, hash, callback, progressCallback) {
158+
function _async(callback) {
159+
if (typeof s !== "string" || typeof hash !== "string") {
160+
nextTick(
161+
callback.bind(
162+
this,
163+
Error("Illegal arguments: " + typeof s + ", " + typeof hash)
164+
)
165+
);
166+
return;
167+
}
168+
if (hash.length !== 60) {
169+
nextTick(callback.bind(this, null, false));
170+
return;
171+
}
172+
bcrypt.hash(
173+
s,
174+
hash.substr(0, 29),
175+
function(err, comp) {
176+
if (err) callback(err);
177+
else callback(null, safeStringCompare(comp, hash));
178+
},
179+
progressCallback
180+
);
181+
}
160182

161-
function _async(callback) {
162-
if (typeof s !== "string" || typeof hash !== "string") {
163-
nextTick(callback.bind(this, Error("Illegal arguments: "+(typeof s)+', '+(typeof hash))));
164-
return;
165-
}
166-
if (hash.length !== 60) {
167-
nextTick(callback.bind(this, null, false));
168-
return;
169-
}
170-
bcrypt.hash(s, hash.substr(0, 29), function(err, comp) {
171-
if (err)
172-
callback(err);
173-
else
174-
callback(null, safeStringCompare(comp, hash));
175-
}, progressCallback);
183+
if (callback) {
184+
if (typeof callback !== "function")
185+
throw Error("Illegal callback: " + typeof callback);
186+
_async(callback);
187+
} else
188+
return new Promise(function(resolve, reject) {
189+
_async(function(err, res) {
190+
if (err) {
191+
reject(err);
192+
return;
176193
}
177-
178-
if (callback) {
179-
if (typeof callback !== 'function')
180-
throw Error("Illegal callback: "+typeof(callback));
181-
_async(callback);
182-
} else
183-
return new Promise(function(resolve, reject) {
184-
_async(function(err, res) {
185-
if (err) {
186-
reject(err);
187-
return;
188-
}
189-
resolve(res);
190-
});
191-
});
192-
};
193-
194+
resolve(res);
195+
});
196+
});
197+
};
194198
```
195199

196200
### Online bcrypt hashing and de-hashing generator and checker
197201

198202
[https://bcrypt-generator.com/](https://bcrypt-generator.com/)
199203

200-
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 )
209205

206+
`bcrypt.hashSync(plainTextPassword, 10)` So the number 10 is the rounds in the above online tool
210207

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.
211209

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

Comments
 (0)