Skip to content

API Documentation

myfoxtail edited this page Oct 6, 2016 · 17 revisions

Before investigating MSA API we recommend to read about MSA Data Types and How MSA stores your objects in the collections.

General Requests

mongoSitesApi.mgoInterface.find(query, projection)


Finds and returns documents in the collection. query specifies the selection filter, projection specifies the fields of selected data to return. Returns Promise object.

mongoSitesApi.mgoInterface
    .find({ "_type": "Car", "country": "Belgium" }, { "limit": 5 })
    .then(function(res) {
        // your code here
    })

mongoSitesApi.mgoInterface.findOne(query, projection)


Finds and returns the first document in the collection according to query, that specifies the selection filter. projection specifies the fields of selected data to return. Returns Promise object.

mongoSitesApi.mgoInterface
    .findOne({ "_type": "Car", "country": { "$in": ["France", "Germany"] }}, { "model": 0 })
    .then(function(res) {
        // your code here
    })

mongoSitesApi.mgoInterface.aggregate([stage1, stage2, stage3, ...])


Performs Mongo stage-based aggregation operations. Method accepts an array of stages for aggregation. Returns Promise object.

mongoSitesApi.mgoInterface
    .aggregate([
        { "$match": { "_type": "Car", "country": "Germany" } },
        { "$group": { "year": "2014" } },
        { "$project": { "_id": "$model", "year": "$year" } }
        { "$limit": 15 }
    ])
    .then(function(res) {
        // your code here
    })

mongoSitesApi.mgoInterface.insert([{}])


Inserts the document or a set of document into a collection. Accepts one document or an array of documents. Returns Promise object.

mongoSitesApi.mgoInterface
    .insert([{ "_type": "Car", "country": "France", "manufacturer": "Bugatti" }])
    .then(function(res) {
        // your code here
    })

mongoSitesApi.mgoInterface.update(query, update, params)


Updates a document or a set of documents in the collection defined by query param. update param defines the modifications to apply. params is an optional object with additional parameters for update (upsert, multi, writeConcern). Returns Promise object.

mongoSitesApi.mgoInterface
    .update(
        { "_type": "Car", "country": "France", "manufacturer": "Bugatti" },
        { "$addToSet": { flags: { last_version: true } } },
        { "multi": true }
    )
    .then(function(res) {
        // your code here
    })

mongoSitesApi.mgoInterface.mapReduce(map, reduce, params)


Performs map-reduce aggregation operations over a collection. Accepts map and reduce functions and an object with additional params. For more details about map-reduce check mongo official documentation https://docs.mongodb.com/manual/reference/method/db.collection.mapReduce/. Returns Promise object.

mongoSitesApi.mgoInterface
    .mapReduce(
        function() { if(this.consumption > 10000) emit(this._id, this.consumption); },
        function(k, v) { return Array.sum(v); },
        { out: "consumption_calculations" }
    )
    .then(function(res) {
        // your code here
    })

mongoSitesApi.mgoInterface.remove(query, params)


Remove a document or a set of documents defined by query in the collection. params is an object with the fields: (justOne, writeConcern). Returns Promise object.

mongoSitesApi.mgoInterface
    .remove({ "_id": "98234jfsdf7864hf9sdss"})
    .then(function(res) {
        // your code here
    })

Misc Requests

mongoSitesApi.graph_add(source_id, destination_id, role)


Adds an object that links two different objects and creates the snapshot of it. This object has a type "link" by default. source parameter is an id of an object itself, destination_id is an id of a subject. role is a string parameter that defines the role of the relationship between object and subject. Returns Promise object.

mongoSitesApi
    .graph_add("c3e4a8c585ce4e1eec00fe53ebb79490", "099f343526d0b3a426ffec55cdca64d3", "Consumtion_has_Building")
    .then(function(res) {
        // your code here
    })

mongoSitesApi.graph_search(query, projection, options)


This function is useful if you want to find a destination object by source object or vice versa. query param is an object which is used to select particular link object and source object to find destination one. projection and options params are similar to which are used in find method. Returns Promise object.

mongoSitesApi
    .graph_search({ role: "Consumtion_has_Building", source: { "value": { "$gte": 145345982 } } })
    .then(function(res) {
        // your code here
    })

mongoSitesApi.graph_remove(id || object)


Removes the link object between two objects. Accepts an object itself or its id. Returns Promise object.

mongoSitesApi
    .graph_remove("c3e4a8c585ce4e1eec00fe53ebb79490")
    .then(function(res) {
        // your code here
    })

mongoSitesApi.graph_update(link_object)


Updates link object and creates the snapshot of it. Returns Promise object.

mongoSitesApi
    .graph_update({"_type": "link", _id: "c3e4a8c585ce4e1eec00fe53ebb79490"})
    .then(function(res) {
        // your code here
    })

mongoSitesApi.listObjectTypes()


Gets all available objects types in the collection. Returns Promise object.

mongoSitesApi
    .listObjectTypes()
    .then(function(res) {
        // your code here
    })