Skip to content

Commit e34a574

Browse files
committed
mongodb indexing
1 parent bed6f5a commit e34a574

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

Javascript/Prototypal-Inheritence/Inheritence-OOP-Class-vs-Prototypes-Example-BEST-1.md renamed to Javascript/Prototypal-Inheritence/Inheritence-OOP-Class-vs-Prototypes-Example-BEST.md

+11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function Dog (name, energy, breed) {
5959
}
6060

6161
Dog.prototype = Object.create(Animal.prototype)
62+
// In the above I could have also used the below format
63+
// Dog.prototype = new Animal()
6264

6365
Dog.prototype.bark = function () {
6466
console.log('Woof Woof!')
@@ -97,5 +99,14 @@ We call `super()` inside of a subclass' constructor method in order to call the
9799

98100
Child class will have access to all parent class methods.
99101

102+
Seperate Note - An important difference between function declarations and class declarations is that function declarations are hoisted and class declarations are not. You first need to declare your class and then access it, otherwise code like the following will throw a ReferenceError:
100103

104+
```
105+
const p = new Rectangle(); // ReferenceError
106+
107+
class Rectangle {}
108+
```
101109

110+
#### Further Reading
111+
- 1. [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)
112+
- 2. [https://tylermcginnis.com/javascript-inheritance-and-the-prototype-chain/](https://tylermcginnis.com/javascript-inheritance-and-the-prototype-chain/)

Javascript/Prototypal-Inheritence/Prototype-Example-1.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
var Person = function(name) {
2-
this.name = name;
2+
this.name = name;
33
};
44

55
Person.prototype.hello = function() {
6-
console.log(`Hello ${this.name} You are part of this PersonProto`);
6+
console.log(`Hello ${this.name} You are part of this PersonProto`);
77
};
88

99
// To define a derived class first , A) Create a new class and call the constructor of the base class explicitly with the `Class.call`. At least one parameter must be passed to that: the current object wih the `this` keyword
1010

1111
var Worker = function(name, job) {
12-
Person.call(this, name);
13-
this.job = job;
12+
Person.call(this, name);
13+
this.job = job;
1414
};
1515
// B) Create the prototype of the derived class, based on the base class. To do that, *hard* copy the prototype of the base class. There's two ways to do this, the commented out line below could also have been used.
1616

1717
// Worker.prototype = Object.create(Person.prototype);
1818

1919
Worker.prototype = new Person();
20+
// In the above I could have also used the below format
21+
// Worker.prototype = Object.create(Person.prototype)
2022

2123
// C)(Re)set the `constructor` property to refer to the new class instead of the (copied) base class.
2224

2325
Worker.prototype.constructor = Worker;
2426

2527
// Overwrite inherited functions or create new ones if you want.
2628
Worker.prototype.hello = function() {
27-
console.log(`Hello ${this.name} You are part of this WorkerProto`);
29+
console.log(`Hello ${this.name} You are part of this WorkerProto`);
2830
};
2931

3032
var p1Proto = new Person("Jack");

MongoDB/indexing-in-mongo.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
An index in MongoDB is a special data structure that holds the data of few fields of documents on which the index is created. Indexes improve the speed of search operations in database because instead of searching the whole document, the search is performed on the indexes that holds only few fields. On the other hand, having too many indexes can hamper the performance of insert, update and delete operations because of the additional write and additional data space used by indexes.
2+
3+
How to create index in MongoDB
4+
Syntax:
5+
6+
`db.collection_name.createIndex({field_name: 1 or -1})`
7+
8+
The value 1 is for ascending order and -1 is for descending order.
9+
10+
For example, I have a collection studentdata. The documents inside this collection have following fields:
11+
student_name, student_id and student_age
12+
13+
Lets say I want to create the index on student_name field in ascending order:
14+
15+
`db.studentdata.createIndex({student_name: 1})`
16+
17+
Output:
18+
19+
```
20+
{
21+
"createdCollectionAutomatically" : false,
22+
"numIndexesBefore" : 1,
23+
"numIndexesAfter" : 2,
24+
"ok" : 1
25+
}
26+
```
27+
28+
#### Finding the indexes in a collection
29+
30+
We can use **getIndexes**() method to find all the indexes created on a collection. The syntax for this method is:
31+
32+
db.collection_name.getIndexes()
33+
So to get the indexes of studentdata collection, the command would be:
34+
35+
```
36+
db.studentdata.getIndexes()
37+
[
38+
{
39+
"v" : 2,
40+
"key" : {
41+
"_id" : 1
42+
},
43+
"name" : "_id_",
44+
"ns" : "test.studentdata"
45+
},
46+
{
47+
"v" : 2,
48+
"key" : {
49+
"student_name" : 1
50+
},
51+
"name" : "student_name_1",
52+
"ns" : "test.studentdata"
53+
}
54+
]
55+
56+
```
57+
58+
The output shows that we have two indexes in this collection. The default index created on _id and the index that we have created on student_name field.
59+
60+
#### Default _id Index
61+
62+
This is the default index which will be created by MongoDB when you create a new collection. If you don’t specify any value for this field, then _id will be primary key by default for your collection so that a user can’t insert two documents with same _id field values. You can’t remove this index from the _id field.

React/HOC.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
To put it simply, a higher-order component is a function, that takes a component and returns a new component. I like to think of them as parameterized components. Many times I find myself creating several components with very similar logic, with only 1 or 2 changes. Once I find a use case like this, it’s very simple to abstract the shared logic, and put the logic that changes into parameters.
22

3-
You can read more about HOCs here, in the official React docs. Since components are just functions and HOCs are just functions that return other functions, we can use functional concepts to chain them using utilities methods such as compose, which is provided by many libraries (it's included in Redux!).
3+
Since components are just functions and HOCs are just functions that return other functions, we can use functional concepts to chain them using utilities methods such as compose, which is provided by many libraries (it's included in Redux!).
44

55
So HOCs are to tackle the situation when I have to share the same functionality across multiple components.
66

0 commit comments

Comments
 (0)