Connecting to MongoDb

mongo mongodb://localhost:25080 --authenticationDatabase "admin" -u "apinizer" -p 
POWERSHELL

It needs to be on the correct database for all operations after connection establish

use apinizerdb
POWERSHELL

General commands

#Show replicaset information
rs.status()

#Show users
db.getUsers()

#Show names and information about collections on current database
db.getCollectionNames()
db.getCollectionInfos()

#Checking any collection's size as MB
db.audit_event.stats().storageSize/1024/1024
POWERSHELL

Searching

db.user.find( { login: "admin" } );

db.db_to_api.find().pretty().limit(10)

#Full text search in API Proxy
db.api_proxy.ensureIndex(
    { "$**": "text" },
    { name: "TextIndex" }
);
 
#Performs a text search in the specified collection.
db.api_proxy.find(
    {
      $text: {
        $search: "XMLRESULTwithNS"
      }
    },{
        "name":1
    }
)  

#Find with criteria
db.api_proxy.find( {importedUrl: /.*kps.*/ })

#Find usage with multiple criteria
db.getCollection('api_proxy').find({$and: [{'routing.kpsSettings': { $exists: true }},{importedUrl: /.*kps.*/ }]})

#Usage of Regex
db.api_proxy.find({ "apiMethodList": { "$elemMatch": { "requestPolicyList": { "$elemMatch": { "name": { "$regex" : /^PolicyJwt3rdAuthentication/i } } } } } });
db.api_proxy.find({ "requestPolicyList": { "$elemMatch": { "name": { "$regex" : /^PolicyJwt3rdAuthentication/i } } } });
db.api_proxy.find({ "requestPolicyList": { "$elemMatch": { "_class": { "$regex" : /.*jwt.*/i } } } } , {"name":1,"requestPolicyList.name":1,"requestPolicyList._class":1});

#Searching in all fields
db.api_proxy.find({ $where: function() {
  for (var key in this) {
    if (JSON.stringify(this[key]).indexOf("172.16.1.1") !== -1) {
      return true;
    }
  }
  return false;
}}, {_id: 0,name:1,projectId:1})


#Get the count of logs within the last 2 hours
mongosh mongodb://localhost:25080/apinizerdb --authenticationDatabase "admin" -u "apinizer" -p "password" --eval 'db.apinizer_log.find({"date":{"$lte": new Date((new Date().getTime() - (2 * 60 * 60 * 1000)))}}).count()'
POWERSHELL

Searching for Which Methods Api Proxies Are Used for a Variable

#Before running the function, the correct database must be switched to with the "use apinizerdb" command.

  function getApiMethodListObjectsWithFilledRequestPolicyList(nameToCheck) {
    let skipCount = 0;
    const limitCount = 1000;
    console.log("Çıktı formatı: Proxy Adı;Metod Adı;Request/Response Hattı;Politika cinsi.")
    console.log("")
    while (true) {
        const cursor = db.api_proxy.find(
            { "apiMethodList": { $exists: true, $ne: [] } }
        ).skip(skipCount).limit(limitCount);

        if (!cursor.hasNext()) {
            break;
        }
        cursor.forEach(doc => {
            doc.apiMethodList.forEach(object => {
                if (object.requestPolicyList && object.requestPolicyList.length > 0) {
                    object.requestPolicyList.forEach(policy => {
                     if (JSON.stringify(policy).includes(nameToCheck)) {
                            console.log(doc.name + ";" + object.name + ";Request;" + policy._class)
                    }
                    });
                }
                if (object.responsePolicyList && object.responsePolicyList.length > 0) {
                    object.responsePolicyList.forEach(policy => {
                        if (JSON.stringify(policy).includes(nameToCheck)) {
                           console.log(doc.name + ";" + object.name + ";Response;" + policy._class)
                        }
                    });
                }
            });
            if (doc.requestPolicyList && doc.requestPolicyList.length > 0) {
                doc.requestPolicyList.forEach(policy => {
                    if (JSON.stringify(policy).includes(nameToCheck)) {
                        console.log(doc.name + ";ALL;Request;" + policy._class)
                    }
                });
            }
            if (doc.responsePolicyList && doc.responsePolicyList.length > 0) {
                doc.responsePolicyList.forEach(policy => {
                    if (JSON.stringify(policy).includes(nameToCheck)) {
                        console.log(doc.name + ";ALL;Response;" + policy._class)
                    }
                });
            }
        });
        skipCount += limitCount;
    }
  console.log("")
}

getApiMethodListObjectsWithFilledRequestPolicyList("KEYWORDTOSEARCH");
JS

Updating

#Subelement update
db.api_proxy.update( {name :"KYS Fetch Foreign First and Last Names GW"}, {$set: {"routing.connectTimeout": "30"}} );

#Updating all "unwrapelement"s to true on a Soap-2-Rest proxy (Result might be 1 which occurs when there is only 1 document but multiple arrays within)
db.api_proxy.updateMany({relativePath:"/tarServis"},{$set:{"apiMethodList.$[].protocolTransformation.unwrapElement":true}});

#Subelement insert (Adds as the second object of the array to the selected object)
db.api_proxy.update( {"relativePath":"/ydsSorgulamaServisi"}, {$push: {"requestPolicyList.1.unPasswordDecrypted": "false"}} );

#Setting admin user's password to "admin"
db.user.update({"login" : "admin"},{$set: { "password" : "$2a$10$Wv6i9IIdNzlxgDdaf13UdOl7uumVcG7zkSEKaOG4Xqn6IlLuwA13e"}});

#Checking a collection and changing the records that fit to specific criteria
db.getCollection('patch_history').find({})
db.patch_history.update({"status" : "RUNNING"},{$set: { "status" : "COMPLETED"}});

db.api_proxy.update( {},
    { 
        "$pull": { 
            "apiProxyDeployList": { 
                "environmentSettingsId": "60e2ad54d5ba7a7636db3aa6" 
            }
        } 
    }, { "multi": true }
);


db.api_proxy.update( {_id : ObjectId("611628a3c21a5c04977e98e2")},
    { 
        "$pull": { 
            "apiProxyDeployList": { 
                "environmentSettingsId": "60e2ad54d5ba7a7636db3aa6" 
            }
        } 
    }, { "multi": true }
);   
POWERSHELL

Deleting

#Deleting an object (Deletes everything according to where statement)
db.privacy_settings.remove({ "salt":"UCPyFvw0EAnOgfOJ" })

#Deleting a sub object (Deletes apiProxyDeployList in api_proxy)
//First row has an empty where statement because we want to delete/update all
//Second row has an empty where statement because we want to delete all apiProxyDeployList's subdocuments
db.api_proxy.update(
  {  },
  {$pull : {"apiProxyDeployList" : {} }}
)

#Deleting collection's whole records (truncate)
db.db_to_api.remove({})

#Dropping the collection
db.db_to_api.drop()

#Dropping the collection outside of the mongodb cli
mongo "mongodb://<IP_ADDRESS>:25080/apinizerdb" --eval 'db.getSiblingDB("admin").auth("apinizer", "PASSWORD"); db.getSiblingDB("apinizerdb").apinizer_log.drop();'

#Dropping the whole database
db.dropDatabase()
POWERSHELL

Replicaset Operations

#Force the replica to sync from master on replicaset systems
rs.syncFrom("hostname:port");
POWERSHELL

Backup and Restore

#Taking backup
sudo mongodump --host <IP_ADDRESS> --port=25080 --username=apinizer --password=<PASSWORD> --authenticationDatabase=admin --gzip --archive=/home/apinizer/apinizer-backup-2023-01-31--1.archive

#Taking backup without some tables which may be big on size
sudo mongodump --host <IP_ADDRESS> --port 25080 --excludeCollection apinizer_log --excludeCollection audit_event -d apinizerdb --authenticationDatabase "admin" -u apinizer -p <PASSWORD> --gzip --archive=/home/apinizer/apinizer-backup-2023-01-31--1.archive

#Restoring the dump/backup
sudo mongorestore --drop --host <IP_ADDRESS> -p 25080 -u apinizer -p <PASSWORD> --authenticationDatabase=admin --gzip --archive=/home/apinizer/apinizer-backup-2023-01-31--1.archive

#Taking backup of one collection
sudo mongodump --host <IP_ADDRESS> --port=25080 --authenticationDatabase "admin" -d apinizerdb -u apinizer -p <PASSWORD> --collection=<COLLECTION_NAME> --out=/home/apinizer/

#Restoring from previously taken single collection backup
sudo mongorestore --drop --host <IP_ADDRESS> --port=25080 --authenticationDatabase "admin" -d apinizerdb -u apinizer -p <PASSWORD> --collection=<COLLECTION_NAME> /home/apinizer/apinizerdb/<collection_name>.bson

#In batch database backups, gzip compression can be used. In such cases, you can use the command below to convert it into a proper archive format and then run the above command again.
gunzip < all-dbs-backup-2023-10-5--02.archive > all-dbs-backup-2023-10-5--02

#Restoring a Single Collection from a Batch Database Backup
mongorestore --host <IP_ADDRESS> --port 25080 --username apinizer --password <PASSWORD> --authenticationDatabase admin --drop --nsInclude 'apinizer-dev.environment_settings' --archive=all-dbs-backup-2023-10-5--02
POWERSHELL

Logs

#Application logs
sudo tail -999f /var/log/mongodb/mongod.log
POWERSHELL

Rebuilding API Proxy in Apinizer and Accessing Elasticsearch Logs

These commands are used to access Elasticsearch logs through the same API Proxy when an API Proxy is deleted and recreated or when export/import operations are performed.

// Store the proxy document in a variable
doc = db.api_proxy.findOne({_id: ObjectId("FALSEPROXYID")})

// Set a new _id
doc._id = ObjectId("SHOULDBEPROXYID")

// Insert document with new _id
db.api_proxy.insert(doc)

// Delete the document with the old _id
db.api_proxy.remove({_id: ObjectId("FALSEPROXYID")})

// Update required for Apinizer
db.api_proxy_revision.update({apiProxyId:"FALSEPROXYID"}, {$set: { apiProxyId:"SHOULDBEPROXYID"}})

// Updating authorizations
db.credential_allowed_api_proxy.updateMany({apiProxyId:"FALSEPROXYID"}, {$set: { apiProxyId:"SHOULDBEPROXYID"}})
POWERSHELL