英文:
How to calculate total cost for an Organization when they call the API?
问题
我已经设计了一个名为“Organization”的集合,如下所示:
其中“name”是组织的名称,“desc”是组织的描述,“id_app”是该“organization”的应用程序数组
1- 以下是“Organization”
{
"_id" : ObjectId("64b4b8bc3975804004fa81b4"),
"id_app" : [
ObjectId("64b4b842a5831248e8a6b65c"),
ObjectId("64b4b34f54106c24dc018992")
],
"is_deleted" : false,
"name" : "Org1",
"desc" : "Org1 desc",
"createdAt" : ISODate("2023-07-17T03:42:52.845+0000"),
"updatedAt" : ISODate("2023-07-17T03:42:52.845+0000"),
"__v" : NumberInt(0)
}
2- 以下是“Application”集合:
其中“id_api”是应用程序调用的“apis”的数组
{
"_id" : ObjectId("64b4b34f54106c24dc018992"),
"id_api" : [
ObjectId("64b4ac07c4784418986870dc"),
ObjectId("64b4abcdc4784418986870d7")
],
"is_deleted" : false,
"name" : "app1",
"desc" : "app1 desc",
"createdAt" : ISODate("2023-07-17T03:19:43.573+0000"),
"updatedAt" : ISODate("2023-07-17T03:19:43.573+0000"),
"__v" : NumberInt(0)
}
3- 以下是“Apis”集合:
其中“name”是API名称,“desc”是API描述,“URL”是API的路径,“field”是API的字段,例如教育API。
{
"_id" : ObjectId("64b4abcdc4784418986870d7"),
"is_deleted" : false,
"name" : "api1",
"desc" : "Api 1 desc",
"URL" : "http://localhost:8080/api/api1",
"field" : "edu",
"createdAt" : ISODate("2023-07-17T02:47:41.849+0000"),
"updatedAt" : ISODate("2023-07-17T02:47:41.849+0000"),
"__v" : NumberInt(0)
}
每当一个“app”调用一个“API”,都会生成一个费用(例如cost1 = 10$),并创建一个名为“Billing”的集合。
4- 以下是“Billing”集合
{
"_id" : ObjectId("64b4c3d6068f5537e4bbd01b"),
"is_deleted" : false,
"cost1" : NumberInt(10),
"cost2" : NumberInt(5),
"cost3" : NumberInt(3),
"id_app" : ObjectId("64b4b842a5831248e8a6b65c"),
"createdAt" : ISODate("2023-07-17T04:30:14.646+0000"),
"updatedAt" : ISODate("2023-07-17T04:30:14.646+0000"),
"__v" : NumberInt(0)
}
当有“Organation”、“Application”、“Apis”和“Billing”集合时,问题是如何计算组织在其应用程序调用API时被收取的费用(总费用)。
非常感谢!
英文:
I have designed a collection named Organization
as follows:
with name
is the name of the organization, desc
is a description of the organization, id_app
is an array of applications for that organization
1- Below is Organization
{
"_id" : ObjectId("64b4b8bc3975804004fa81b4"),
"id_app" : [
ObjectId("64b4b842a5831248e8a6b65c"),
ObjectId("64b4b34f54106c24dc018992")
],
"is_deleted" : false,
"name" : "Org1",
"desc" : "Org1 desc",
"createdAt" : ISODate("2023-07-17T03:42:52.845+0000"),
"updatedAt" : ISODate("2023-07-17T03:42:52.845+0000"),
"__v" : NumberInt(0)
}
2- Bellow is Application
collection:
with id_api
is an array of the apis
that application calls
{
"_id" : ObjectId("64b4b34f54106c24dc018992"),
"id_api" : [
ObjectId("64b4ac07c4784418986870dc"),
ObjectId("64b4abcdc4784418986870d7")
],
"is_deleted" : false,
"name" : "app1",
"desc" : "app1 desc",
"createdAt" : ISODate("2023-07-17T03:19:43.573+0000"),
"updatedAt" : ISODate("2023-07-17T03:19:43.573+0000"),
"__v" : NumberInt(0)
}
3- Bellow is Apis
collection:
with name
is the API name, desc
is the api description, URL
is the path of the API and
field
is the field of that api, for example education api.
{
"_id" : ObjectId("64b4abcdc4784418986870d7"),
"is_deleted" : false,
"name" : "api1",
"desc" : "Api 1 desc",
"URL" : "http://localhost:8080/api/api1",
"field" : "edu",
"createdAt" : ISODate("2023-07-17T02:47:41.849+0000"),
"updatedAt" : ISODate("2023-07-17T02:47:41.849+0000"),
"__v" : NumberInt(0)
}
Each time an app
calls an API
, a fee will be generated (eg cost1 = 10$ ), and a collection named Billing
will be created.
4- And below is Billing
collection
{
"_id" : ObjectId("64b4c3d6068f5537e4bbd01b"),
"is_deleted" : false,
"cost1" : NumberInt(10),
"cost2" : NumberInt(5),
"cost3" : NumberInt(3),
"id_app" : ObjectId("64b4b842a5831248e8a6b65c"),
"createdAt" : ISODate("2023-07-17T04:30:14.646+0000"),
"updatedAt" : ISODate("2023-07-17T04:30:14.646+0000"),
"__v" : NumberInt(0)
}
While there are Organation
, Application
, Apis
, and Billing
collections. So the question is, how to calculate the billing amount (total cost) that an organization is charged when their apps call the API.
Thanks you so much
答案1
得分: 1
- 你可以使用组织ID从组织集合中获取应用程序ID。
db.getCollection('organization').find({_id: ObjectId("64b4b8bc3975804004fa81b4")});
- 然后,你可以使用应用程序ID进行聚合操作。
db.getCollection('Billing').aggregate([
{
$match: {
id_app: {
$in: [ObjectId("64b4b842a5831248e8a6b65c"), ObjectId("64b4b34f54106c24dc018992")]
}
}
},
{
$group: {
_id: null, totalCost: {
$sum: "$cost1"
}
}
}
]);
- 结果如下:
{
"_id" : null,
"totalCost" : 10
}
- 你可以使用以下查询来计算总成本。
db.getCollection('Billing').aggregate([
{
$match: {
id_app: {
$in: [ObjectId("64b646f602dbe6f481912d85"), ObjectId("64b4b34f54106c24dc018992")]
}
}
},
{
$project: {
totalCost: {
$sum: ["$cost1", "$cost2", "$cost3"]
}
}
}
]);
英文:
- You can app ids from organization collection using organization id
db.getCollection('organization').find({_id: ObjectId("64b4b8bc3975804004fa81b4")});
- And next you can use aggregate using application ids
db.getCollection('Billing').aggregate([
{
$match: {
id_app: {
$in: [ObjectId("64b4b842a5831248e8a6b65c"), ObjectId("64b4b34f54106c24dc018992")
]
}
}
},
{
$group: {
_id: null, totalCost: {
$sum: "$cost1"
}
}
}
]);
- The result
{
"_id" : null,
"totalCost" : 10
}
- You can use following query to calculate total cost in a unique query
db.getCollection('Billing').aggregate([
{
$match: {
id_app: {
$in: [ObjectId("64b646f602dbe6f481912d85"), ObjectId("64b4b34f54106c24dc018992")
]
}
}
},
{
$project: {
totalCost: {
$sum: ["$cost1", "$cost2", "$cost3"]
}
}
}
]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论