Collections

Documentation of the Collection class in abc.db

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 and in these examples, I will be using the same variable to call them from as the examples in the Main Class documentation.

Using Collections

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

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

v1

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

Methods

All methods are promises currently, meaning they must be awaited (more info here)

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.

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.

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:

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

Subtract:

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.

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.

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

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.

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

Delete All Method

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.

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

Collection.deleteAll() //deletes everything.

Other Methods

For Each Method

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

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

Last updated