Skip to content

An alternative of the deprecated MongoDB API using Flask

Notifications You must be signed in to change notification settings

Kiles-Comissions/mongo-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongo api

A clone of the deprecated mongoDB API which allows modifying you database through an API

General request body

Generally, every request needs collection argument. This is so it knows which collection to apply the search/modification to. The Database will always be the one specified when starting the API because it is assumed it will always be the same. Below you can find a destinction between the two:

image

So every request will need

{
  "collection": "<collection_name>"
}

in it's request body. I won't include this in the request details below so keep that in mind. You can also optionally specify the database in the request body if you want to change the database from the default one.

Tip

Generally the difference between operationOne and operationMany is that the former will only apply to the first document that matches the filter, while the latter will apply to all documents that match the filter. For find one will also return a single JSON object, while the other will return an array of JSON objects. Generally only use operationOne when the filter is unique, such as an ID, otherwise use operationMany.

The request is meant to mirror the actual mongodb operation as closely as possible so what mongoDB returns is what you should expect from the API. This also allows for the use of special mongodb operators in the request body. Where applicable, I will provide a link to the documentation for the operators used. You only need to use these for update ($set) and aggregate ($match, $group, etc) operations.

Individual request bodies

Index

/find

type: POST

Request

{
  "filter": {
    "key": "value"
  }
}

Response

[
    {
        "key": "value", // It will return all the documents that match the filter
        "other_data": "other_value"
        // ...
    },
    {
        "key": "value",
        "other_data": "other_value"
        // ...
    }
]

Example

{
  "collection": "users",
  "filter": {
    "name": "John",
    "age": {
        "$gt": 20
    }
  }
}

Note

You can use special mongodb search operators in the filter object. For example, the $gt operator in the example above. You can find more about these operators here

/findOne

type: POST

Request

{
  "filter": {
    "key": "value"
  }
}

Response

{
    "key": "value", // It will return the first document that matches the filter
    "other_data": "other_value"
    // ...
}

Example

{
  "collection": "users",
  "filter": {
    "name": "John",
    "age": {
        "$gt": 20
    }
  }
}

Note

You can use special mongodb search operators in the filter object. For example, the $gt operator in the example above. You can find more about these operators here

/insertOne

type: POST

Request

{
  "document": {
    "key": "value"
  }
}

Response

{
    "_id": "676fe5b60fa547b0c8fa8148" // The generated id of the inserted document
}

Example

{
  "collection": "users",
  "document": {
    "name": "John",
    "age": 25
  }
}

/insertMany

type: POST

Request

{
  "documents": [
    {
      "key": "value"
    },
    {
      "key": "value"
    }
  ]
}

Response

[
    {
        "_id": "676fe5b60fa547b0c8fa8148" // The generated id of the first inserted document
    },
    {
        "_id": "676fe5b60fa547b0c8fa8149" // The generated id of the second inserted document
    }
]

Example

{
  "collection": "users",
  "documents": [
    {
      "name": "John",
      "age": 25
    },
    {
      "name": "Jane",
      "age": 30
    }
  ]
}

/updateOne

type: POST

Request

{
  "filter": {
    "key": "value"
  },
  "update": {
    "$set": {
      "key": "value"
    }
  }
}

Response

{
    "$clusterTime": {
        "clusterTime": 1735386694,
        "signature": {
            "hash": "<hash>",
            "keyId": 7393337599319867616
        }
    },
    "electionId": "7fffffff0000000000000416",
    "n": 1, // The number of documents that were found with the filter
    "nModified": 1, // The number of documents that were modified
    "ok": 1.0, // If the operation was successful
    "opTime": { 
        "t": 1046,
        "ts": 1735386694
    },
    "operationTime": 1735386694, // The time the operation was executed
    "updatedExisting": true 
}

Example

{
  "collection": "users",
  "filter": {
    "name": "John"
  },
  "update": {
    "$set": {
      "name": "Johnny"
    },
    "$inc": { // You can use multiple update operators
      "age": 1
    }
  }
}

Note

You can use special mongodb update operators in the update object. For example, the $set and $inc operators in the example above. You can find more about these operators here

/updateMany

type: POST

Request

{
  "filter": {
    "key": "value"
  },
  "update": {
    "$set": {
      "key": "value"
    }
  }
}

Response

{
    "$clusterTime": {
        "clusterTime": 1735386694,
        "signature": {
            "hash": "<hash>",
            "keyId": 7393337599319867616
        }
    },
    "electionId": "7fffffff0000000000000416",
    "n": 1, // The number of documents that were found with the filter
    "nModified": 1, // The number of documents that were modified
    "ok": 1.0, // If the operation was successful
    "opTime": { 
        "t": 1046,
        "ts": 1735386694
    },
    "operationTime": 1735386694, // The time the operation was executed
    "updatedExisting": true 
}

Example

{
  "collection": "users",
  "filter": {
    "name": "John"
  },
  "update": {
    "$set": {
      "name": "Johnny"
    },
    "$inc": { // You can use multiple update operators
      "age": 1
    }
  }
}

Note

You can use special mongodb update operators in the update object. For example, the $set and $inc operators in the example above. You can find more about these operators here

/deleteOne

type: POST

Request

{
  "filter": {
    "key": "value"
  }
}

Response

{
    "$clusterTime": {
        "clusterTime": 1735229323,
        "signature": {
            "hash": "<hash>",
            "keyId": 7393337599319867616
        }
    },
    "electionId": "7fffffff0000000000000416",
    "n": 1, // The number of documents that were found with the filter
    "ok": 1.0, // If the operation was successful
    "opTime": {
        "t": 1046,
        "ts": 1735229323
    },
    "operationTime": 1735229323 // The time the operation was executed
}

Example

{
  "collection": "users",
  "filter": {
    "name": "John"
  }
}

/deleteMany

type: POST

Request

{
  "filter": {
    "key": "value"
  }
}

Response

{
    "$clusterTime": {
        "clusterTime": 1735229323,
        "signature": {
            "hash": "<hash>",
            "keyId": 7393337599319867616
        }
    },
    "electionId": "7fffffff0000000000000416",
    "n": 1, // The number of documents that were found with the filter
    "ok": 1.0, // If the operation was successful
    "opTime": {
        "t": 1046,
        "ts": 1735229323
    },
    "operationTime": 1735229323 // The time the operation was executed
}

Example

{
  "collection": "users",
  "filter": {
    "name": "John"
  }
}

aggregate

Request

{
  "pipeline": [
    {
      "$match": {
        "key": "value"
      }
    },
    {
      "$group": {
        "_id": "$key",
        "count": {
          "$sum": 1
        }
      }
    }
  ]
}

Response

[
    {
        "_id": "value",
        "count": 1
    }
]

Example

{
  "collection": "users",
  "pipeline": [
    {
      "$match": {
        "name": "John"
      }
    },
    {
      "$group": {
        "_id": "$name",
        "count": {
          "$sum": 1
        }
      }
    }
  ]
}

Note

You can use special mongodb aggregation operators in the pipeline array. For example, the $match and $group operators in the example above. You can find more about these operators here

About

An alternative of the deprecated MongoDB API using Flask

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages