Skip to content

MongoDB Service (Stack9-core v3.0)

Overview

MongoDB is the world's most widely used document-based NoSQL database and, in recent years, is becoming a viable alternative to relational databases.

Stack9 Core started using MongoDB from v2.5+, initially to store User Notification, yet it can be incorporate to others features.

For more information about User Notification, see Notifications ⇗

Usage

Here are some examples of query operations using MongoDB. The examples are using the user_notification collection.

Parameters

  • Collections: A group of documents, a collection exists within a single database.

  • Query: The query operator forces MongoDB to interpret an expression as a query, query is necessary to work with documents that contain a field name query.

  • Filter: Means selecting only the necessary data rather than selecting whole of the data of the document.

  • Options: Extra options to modify the method, it is optional.

find(collectionName, filter, options?):

The find() method is used to select documents in a collection and return a cursor to the selected documents.

mongoDBService.find(
    'user_notifications', 
    {
        seen: false,
        userId: 1
    },
    { 
        limit: 5 
    }
);

findOne(collectionName, filter, options?):

The findOne() method returns a single document from a collection or view.

mongoDBService.findOne(
    'user_notifications', 
    {
        seen: false,
        userId: 1,
    }, 
    {
        skip: 1
    }
);

count(collectionName, filter, options?):

The count() method does not perform the find() operation but instead counts and returns the number of results that match a query.

mongoDBService.count(
    'user_notifications', 
    {
        seen: false,
        userId: 1
    }
);

createCollection(collectionName, options?):

This method is used for creating new collections that use specific options.

mongoDBService.createCollection(
    'user_notifications', 
    {
        template: {
            icon: ,
            title: ,
            message ,
            due_date?: 
        }
    }
);

insertOne(collectionName, values):

Insert a single document into a collection and return the _id of the inserted document.

mongoDBService.insertOne(
    'notifications',
    {
        userId: 1,
        entityId: 2,
        content: 'message'
    }
);

insertMany(collectionName, docs, options?):

It takes an array of documents to insert in the collection.

mongoDBService.insertMany(
    'user_notification',
    [{
        userId: 1,
        content: ['content1'], ['content2']
    }]
);

bulkWrite(collectionName, operations, options?):

It is a method builder which is used to construct a list of write operations to perform in bulk for many collections.

mongoDBService.bulkWrite(
    'user_notification', 
    [{
        updateOne: {
        filter: {
          userId: 1,
        },
        update: { 
                $set: {    // data that we want to update
                     seen: true,
                     desc: 'Description has been updated' 
                } 
        },
        upsert: true,  // Optional. A boolean to indicate whether to perform an upsert. By default, upsert is false.
      }
    }]
);

deleteOne(collectionName, filter, options?):

Deletes the first document that matches the filter, use a field such as id for precise operations.

mongoDBService.deleteOne(
    'user_notification', 
    {
        _id: ObjectId("563237a41a4d68582c2509da")
    }
);

deleteMany(collectionName, filter, options?):

Remove all documents that match the filter from a collection.

mongoDBService.deleteMany(
    'user_notification', 
    {
        userId: 1
    }  
);

For more information, please visit oficial documentation: MongoDB website ⇗