MongoDB中SQL中减集操作的等效操作是啥?
Posted
技术标签:
【中文标题】MongoDB中SQL中减集操作的等效操作是啥?【英文标题】:what is the equivalent operation in MongoDB for Minus set operation in SQL?MongoDB中SQL中减集操作的等效操作是什么? 【发布时间】:2017-05-24 18:32:53 【问题描述】:我有以下 MongoDB 集合:
feb = [
"_id" : ObjectId("591df31cbbcd51d52653553c"),
"patientId" : 2,
"fullName" : "Lopez , Julia",
"EVOLUCIÓN" : "Medicada"
, ...]
may = [
"_id" : ObjectId("59231f3ab36e83e3db6e03f8"),
"patientId" : 4,
"fullName" : "Martinez , Carolina",
"EVOLUCIÓN" : "15/08/97 : MEDICADA CON DERRUMAL A TERMINAR, TOMANIL GEL, ADAXIL AMP, ZOCOR 5, RENITEC 5, CERVILANE, Y AMLOC 5."
, ...]
我需要使用 MongoDB 查询获取 feb 的投影 - 可能超过 fullName。 在 SQL 中是:
SELECT fullName
FROM feb
MINUS
SELECT fullName
FROM may;
【问题讨论】:
有人解决了您的问题吗?如果是这样,您能否接受最佳答案(单击点下的复选标记)。这将帮助遇到您的问题的其他用户快速发现接受的答案,并且还提供 15 个代表。指向作者(: 【参考方案1】:您不能在MongoDB
中一次跨多个集合进行查询。您必须在应用程序级别使用两个单独的查询来模拟 MINUS
:
首先,你会得到五月的名字:
namesInMay = db.may.distinct("fullName");
然后,您对二月执行相同操作,但使用 $nin 运算符过滤掉那些也在 namesInMay
中的:
db.feb.distinct("fullName", "fullName": $nin: namesInMay );
【讨论】:
【参考方案2】:试试这样的...
db.address.aggregate([
$replaceRoot:
newRoot: _id: "$_id", source: $objectToArray: "$$ROOT"
,
$lookup:
from: "address1",
localField: "_id",
foreignField: "_id",
as: "target"
,
$replaceRoot:
newRoot: _id: "$_id", source: "$source", target : $objectToArray: $first : "$target"
,
$replaceRoot:
newRoot: _id: "$_id", source: $setDifference: ["$source","$target"], target: $setDifference: ["$target","$source"]
]);
如果集合在同一个数据库中,并且具有相同的“_id”,这将起作用。它可以很容易地修改以加入其他标准。
输出看起来像这样..
"_id" : ObjectId("5f8dc3abc6ff46000f023970"), "source" : [ "k" : "postal_code", "v" : "" , "k" : "phone", "v" : "" ], "target" : [ "k" : "postal_code", "v" : null , "k" : "phone", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023971"), "source" : [ "k" : "postal_code", "v" : "" , "k" : "phone", "v" : "" ], "target" : [ "k" : "postal_code", "v" : null , "k" : "phone", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023972"), "source" : [ "k" : "postal_code", "v" : "" ], "target" : [ "k" : "postal_code", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023973"), "source" : [ "k" : "postal_code", "v" : "" ], "target" : [ "k" : "postal_code", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023974"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023975"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023976"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023977"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023978"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023979"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f02397a"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f02397b"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f02397c"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f02397d"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f02397e"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f02397f"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023980"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023981"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023982"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
"_id" : ObjectId("5f8dc3abc6ff46000f023983"), "source" : [ "k" : "address2", "v" : "" ], "target" : [ "k" : "address2", "v" : null ]
Type "it" for more
【讨论】:
以上是关于MongoDB中SQL中减集操作的等效操作是啥?的主要内容,如果未能解决你的问题,请参考以下文章