# Collections

Collections in abc.db represent those inside mongo db, and every collection you make in your code will be a new one inside your mongo db.\
\
The collection variable is stored inside the [Main Class](https://abcdb.js.org/docs/main) and in these examples, I will be using the same variable to call them from as the examples in the[ Main Class](https://abcdb.js.org/docs/main) documentation.

## Using Collections

To use the collection, call it from the Main class:

```javascript
const Collection = DB.Collection("your collection name");
```

### v1

```javascript
const myCollection = new DB.Collection("your collection name");
```

## Methods

All methods are promises currently, meaning they must be awaited ([more info here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise))

## Set Methods

These methods are used to insert of modify data inside the database. They all return the new value of the key

### Set Method

To set a value to something, you use the set method and provide a key and a value to set.

```javascript
Collection.set("your key", "your value")
```

### Dot Notation

All the data modifying methods (set, add, subtract, push) and the get/find method support dot notation, similar to quick.db e.g.

```javascript
Collection.set("key.thing", "example"); 
/*this key's value will be 
{
thing: "example"
}
*/
```

### Add/Subtract Methods

To add and subtract from numbers, you use the subtract method. If the value is not a number or does not exist, then the value is set to add/subtract from 0.

#### Add:

```javascript
Collection.add("key which has number value", 1);
//if key was 5 it will now be 6
```

#### Subtract:

```javascript
Collection.subtract("key which has number value", 1);
//if the key was 5 it will now be 4
```

### Push Method

The push method pushes an item into an array, meaning it is put at the end. If the value is not an array, it will be made one with the current value being first, the second value being next.

```javascript
Collection.push("key which has array value", 2);
//If they array was [0, 1] it will now be [0, 1, 2]
```

## Finding Methods

### Get/Find/Has Methods

The get/find and has methods work similarly, except one returns the value, the other returns whether or not it exists. In both, if the key does not have a value, it will return false. The find and get method are exactly the same.

```javascript
Collection.get("key") //if key has the value 5, it will return 5
Collection.has("key") //assuming the same as above, it will return true
```

### Get All Method

The getAll method returns all the documents inside the database as an array of objects containing a key and a value

```javascript
Collection.getAll() //[ { key: "key", value: "value" }, { key: "number", value: 1 } ]
```

## Deletion Methods

### Delete Method

The delete method removes a document from the database, and returns a boolean of whether or not it could be deleted.

```javascript
Collection.delete("key that exists") //true
Collection.delete("key that doesn't exist") //false
```

### Delete All Method

{% hint style="warning" %}
Warning, if you execute this method, as it suggests, **every document inside your collection will be deleted**. Only run this if you are **absolutely** sure that you want **all** of your documents deleted.
{% endhint %}

The delete all method deletes everything inside the collection, meaning nothing can be recovered

```javascript
Collection.deleteAll() //deletes everything.
```

## Other Methods

### For Each Method

The for each method executes something for every entry in the database

```javascript
Collection.forEach(entry => {
    console.log(entry);
}) //logs every entry inside the db
```

### Migrate From Quick DB Method

The migrate from quick.db method moves everything over from quick.db\
For more info [see here](https://abcdb.js.org/examples/moving-from-quick.db)
