mongodb - 集合上的差分操作
问题描述
我们有以下收藏:
要求:
{ "requestid" : 'R1', "stocks" : [{"scripid" : "xxxxx1" }, {"scripid" : "xxxxx2" },{"scripid" : "xxxxx3" },{"scripid" : "xxxxx4" }] }
{ "requestid" : 'R2', "stocks" : [{"scripid" : "xxxxx0" }] }
股票:
{"scripid" : "xxxxx2" }
{"scripid" : "xxxxx3" }
{"scripid" : "..." }
我们希望将请求的票据与股票收集进行比较 - 并返回不属于股票收集的一部分的请求(和票据)。
所以mongo操作的结果会是:
{ "requestid" : 'R1', "stocks" : [{"scripid" : "xxxxx1" }, {"scripid" : "xxxxx4" }] }
{ "requestid" : 'R2', "stocks" : [{"scripid" : "xxxxx0" }] }
如何以最有效的方式实现这一点。
我们正在使用 java 驱动程序 3.7.xxx
解决方案
我相信这可以解决问题:
db.requests.aggregate([{
$lookup: {
from: "stocks",
localField: "stocks.scriptid",
foreignField: "scriptid",
as: "matched_stocks"
}
}, {
$project: {
"matched_stocks._id": 0
}
}, {
$project: {
request_id: 1,
stocks: {
$setDifference: ["$stocks", "$matched_stocks"]
}
}
}])
编辑:
正如下面@MrS.Sharma 所建议的,您可以Aggregates.project(Projections.computed("stocks", Document.parse("{ $setDifference: ['$stocks', '$matched_stocks'] }")))
使用$setDifference
.
编辑2:
我想这就是实际的完整答案:
AggregateIterable<Document> documents =
requests.aggregate(
Arrays.asList(
Aggregates.lookup("stocks", "stocks.scrip", "scrip", "matched_stocks"),
Aggregates.project(Projections.exclude("matched_stocks._id")),
Aggregates.project(
Projections.fields(
Projections.include("requestid"),
Projections.computed(
"stocks",
Document.parse(
"{ $setDifference: ['$stocks', '$matched_stocks'] }"))))));
推荐阅读
- python - 如何将值分配给字典中的键?解决算法
- c# - HttpClient GetAsync 耗时约 2 秒
- c# - HttpClient.PostAsJsonAsync 无法从 ASPX 工作的问题
- postman - DocuSignAPI - 通过 API 签署信封
- r - 如何循环遍历参数值、运行函数和保存结果
- python - 无法通过 del 函数删除 DataFrame Pandas 中的列
- python - 多包 Python 项目发布管道
- r - R: ggplot 在聚类分析后可视化每个聚类中的所有变量
- microcontroller - 使用 Lauterbach for Multicore 控制器在启动时设置断点失败
- vega-lite - 在 Vega-lite 中画笔放大 X 和 Y