从 mongodb 访问数据
Accessing data from mongodb
我有以下类型的请求。
server/controllerName/access_id/id/field/field_value/api_name
server/controllerName/access_id/id/field/field_value/field2/field_value/api_name
几个字段示例:
1. start date and end date
2. user name
3. user group
Mongo 数据库数据格式:
Mongo 数据库存储用户订单详情。每个订单包含用户详情[10个字段]和订单详情[30个字段]
如果未提及日期,API 必须默认提供最后 30 天的订单。
我的问题:
如何有效地从 mongo 数据库中读取这些数据?
我现在在做什么:
我正在解析 httprequest
并将这些字段添加到 map
{"name":"gibbs", "category":"vip"}
我必须获取这两个字段匹配的所有文档和return以下格式的文档。
{
user: "gibbs",
total_Result: 10,
[
{
//order details items from doc 1
}
{
//order details from doc2
}
]
}
我正在查询 mongo 数据库,如下所示。
MongoCollection<Document> mongoEngCollection = mongoDbReader.getCollection();
BasicDBObject andQuery = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
//Forming query using request parameter. requestAttributes contains map of request parameters.
for(Map.Entry<PathAttribute, PathValue<?>> entry : requestAttributes.entrySet()) {
String key = entry.getKey().getName();
obj.add(new BasicDBObject(key, entry.getValue().getRawValue()));
}
andQuery.put("$and", obj);
//Queryng mongo db
FindIterable<Document> documents = mongoEngCollection.find(andQuery);
然后我迭代文档并将字段分组为所需格式。
使用spring编写。
我可以接受架构更改、查询更改、注释方法,只要它非常快速且概念正确即可。
请多多指教
这可以使用 MongoDB 中的投影和查找来实现。
应用自联接 https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/
限制携带物品https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/
希望您能轻松地了解如何编写查询。
您必须使用聚合框架。静态导入 helper classes 的所有方法并使用下面的代码。
在较新的 3.x 驱动程序 api 中不推荐使用 BasicDBObject
。您应该使用新的 class Document
来满足类似的需求。
import static com.mongodb.client.model.Accumulators.*;
import static com.mongodb.client.model.Aggregates.*;
import static java.util.Arrays.asList;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Projections.*;
import org.bson.conversions.Bson;
MongoCollection<Document> mongoEngCollection = mongoDbReader.getCollection();
List<Bson> obj = new ArrayList<>();
//Forming query using request parameter. requestAttributes contains map of request parameters.
for(Map.Entry<PathAttribute, PathValue<?>> entry : requestAttributes.entrySet()) {
String key = entry.getKey().getName();
//Check if key is not date and set the start and end date
obj.add(eq(key, entry.getValue().getRawValue()));
}
//Build aggregation stages
Bson match = match(and(obj));
Bson group = group(
first("user", "$name"),
sum("total_results", 1),
push("results", "$$ROOT")
);
Bson projection = project(fields(excludeId()));
//Query Mongodb
List<Document> results = mongoEngCollection .aggregate(asList(match, group, projection)).into(new ArrayList<Document>());
这里有更多关于聚合的信息https://docs.mongodb.com/manual/reference/operator/aggregation/
我有以下类型的请求。
server/controllerName/access_id/id/field/field_value/api_name
server/controllerName/access_id/id/field/field_value/field2/field_value/api_name
几个字段示例:
1. start date and end date
2. user name
3. user group
Mongo 数据库数据格式:
Mongo 数据库存储用户订单详情。每个订单包含用户详情[10个字段]和订单详情[30个字段]
如果未提及日期,API 必须默认提供最后 30 天的订单。
我的问题:
如何有效地从 mongo 数据库中读取这些数据?
我现在在做什么:
我正在解析 httprequest
并将这些字段添加到 map
{"name":"gibbs", "category":"vip"}
我必须获取这两个字段匹配的所有文档和return以下格式的文档。
{
user: "gibbs",
total_Result: 10,
[
{
//order details items from doc 1
}
{
//order details from doc2
}
]
}
我正在查询 mongo 数据库,如下所示。
MongoCollection<Document> mongoEngCollection = mongoDbReader.getCollection();
BasicDBObject andQuery = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
//Forming query using request parameter. requestAttributes contains map of request parameters.
for(Map.Entry<PathAttribute, PathValue<?>> entry : requestAttributes.entrySet()) {
String key = entry.getKey().getName();
obj.add(new BasicDBObject(key, entry.getValue().getRawValue()));
}
andQuery.put("$and", obj);
//Queryng mongo db
FindIterable<Document> documents = mongoEngCollection.find(andQuery);
然后我迭代文档并将字段分组为所需格式。
使用spring编写。
我可以接受架构更改、查询更改、注释方法,只要它非常快速且概念正确即可。
请多多指教
这可以使用 MongoDB 中的投影和查找来实现。 应用自联接 https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/ 限制携带物品https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/
希望您能轻松地了解如何编写查询。
您必须使用聚合框架。静态导入 helper classes 的所有方法并使用下面的代码。
在较新的 3.x 驱动程序 api 中不推荐使用 BasicDBObject
。您应该使用新的 class Document
来满足类似的需求。
import static com.mongodb.client.model.Accumulators.*;
import static com.mongodb.client.model.Aggregates.*;
import static java.util.Arrays.asList;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Projections.*;
import org.bson.conversions.Bson;
MongoCollection<Document> mongoEngCollection = mongoDbReader.getCollection();
List<Bson> obj = new ArrayList<>();
//Forming query using request parameter. requestAttributes contains map of request parameters.
for(Map.Entry<PathAttribute, PathValue<?>> entry : requestAttributes.entrySet()) {
String key = entry.getKey().getName();
//Check if key is not date and set the start and end date
obj.add(eq(key, entry.getValue().getRawValue()));
}
//Build aggregation stages
Bson match = match(and(obj));
Bson group = group(
first("user", "$name"),
sum("total_results", 1),
push("results", "$$ROOT")
);
Bson projection = project(fields(excludeId()));
//Query Mongodb
List<Document> results = mongoEngCollection .aggregate(asList(match, group, projection)).into(new ArrayList<Document>());
这里有更多关于聚合的信息https://docs.mongodb.com/manual/reference/operator/aggregation/