有2个集合,每个集合包含以下详细信息:
Collection 1(prpackages):{Server,Package,Version}
集合2(qapackages):{服务器,包,版本}。
我需要从集合1中获取软件包名称,并使用版本号(字符串字段)将其与集合2上的相同软件包进行比较。
我在聚合中使用$lookup
来合并2个集合,匹配的包作为数组追加,但是当我尝试使用$match
比较字段和子数组字段的字符串值时,它返回一个无值。
MongoDB Enterprise> db.prpackages.find()
{" _id" :ObjectId(" 596e627f392ae96de6a40762")," server" : " ln1244567"," installedTime" :" 1393542346"," package" : " hd-str-sa-config-sysctl_conf","版本" :" 1"}
{" _id" :ObjectId(" 596e627f392ae96de6a40763")," server" : " ln1244567"," installedTime" :" 1393542347"," package" : " hd-str-sa-config-authorized_keys","版本" :" 1"}
{" _id" :ObjectId(" 596e627f392ae96de6a40764")," server" : " ln1244567"," installedTime" :" 1393542348"," package" : " hd-str-sa-config-ntp_conf","版本" :" 1"}
{" _id" :ObjectId(" 596e627f392ae96de6a40765")," server" : " ln1244567"," installedTime" :" 1393542350"," package" : " hd-str-sa-config-rootcron","版本" :" 1"}
{" _id" :ObjectId(" 596e6280392ae96de6a40766")," server" : " ln1244567"," installedTime" :" 1393542352"," package" : " hd-str-sa-config-ntpd","版本" :" 1"}
MongoDB Enterprise> db.qapackages.find()
{" _id" :ObjectId(" 596e630c392ae96de6a40776")," server" : " ln1244234"," installedTime" :" 1399324250"," package" : " hd-str-sa-config-sysctl_conf","版本" :" 1"}
{" _id" :ObjectId(" 596e630c392ae96de6a40777")," server" : " ln1244234"," installedTime" :" 1399324251"," package" : " hd-str-sa-config-authorized_keys","版本" :" 1"}
{" _id" :ObjectId(" 596e630d392ae96de6a40778")," server" : " ln1244234"," installedTime" :" 1399324252"," package" : " hd-str-sa-config-ntp_conf","版本" :" 1.2.1"}
{" _id" :ObjectId(" 596e630d392ae96de6a40779")," server" : " ln1244234"," installedTime" :" 1399324254"," package" : " hd-str-sa-config-rootcron","版本" :" 2"}
{" _id" :ObjectId(" 596e630e392ae96de6a4077a")," server" : " ln1244234"," installedTime" :" 1399324255"," package" : " hd-str-sa-config-ntpd","版本" :" 1"}
使用聚合和查找合并,结果如下:
MongoDB Enterprise > db.prpackages.aggregate([ {$lookup: {from: "qapackages", localField: "package", foreignField: "package", as: "qacoll"}} ])
{" _id" :ObjectId(" 596e627f392ae96de6a40762")," server" : " ln1244567"," installedTime" :" 1393542346"," package" : " hd-str-sa-config-sysctl_conf"," versionNum" :" 1"," qacoll" :[{ " _id" :ObjectId(" 596e630c392ae96de6a40776")," server" :" ln1244234", " installedTime" :" 1399324250"," package" : " hd-str-sa-config-sysctl_conf"," versionNum" :" 1" ]}
{" _id" :ObjectId(" 596e627f392ae96de6a40763")," server" : " ln1244567"," installedTime" :" 1393542347"," package" : " hd-str-sa-config-authorized_keys"," versionNum" :" 1"," qacoll" :[{ " _id" :ObjectId(" 596e630c392ae96de6a40777")," server" :" ln1244234", " installedTime" :" 1399324251"," package" : " hd-str-sa-config-authorized_keys"," versionNum" :" 1"}]}
{" _id" :ObjectId(" 596e627f392ae96de6a40764")," server" : " ln1244567"," installedTime" :" 1393542348"," package" : " hd-str-sa-config-ntp_conf"," versionNum" :" 1"," qacoll" :[{" _id" :ObjectId(" 596e630d392ae96de6a40778")," server" :" ln1244234", " installedTime" :" 1399324252"," package" : " hd-str-sa-config-ntp_conf"," versionNum" :" 1.2.1"}]}
{" _id" :ObjectId(" 596e627f392ae96de6a40765")," server" : " ln1244567"," installedTime" :" 1393542350"," package" : " hd-str-sa-config-rootcron"," versionNum" :" 1"," qacoll" :[{" _id" :ObjectId(" 596e630d392ae96de6a40779")," server" :" ln1244234", " installedTime" :" 1399324254"," package" : " hd-str-sa-config-rootcron"," versionNum" :" 2"}]}
{" _id" :ObjectId(" 596e6280392ae96de6a40766")," server" : " ln1244567"," installedTime" :" 1393542352"," package" : " hd-str-sa-config-ntpd"," versionNum" :" 1"," qacoll" :[{" _id" : ObjectId(" 596e630e392ae96de6a4077a")," server" :" ln1244234", " installedTime" :" 1399324255"," package" :" hd-str-sa-config-ntpd", " versionNum" :" 1"}]}
查询:
使用值进行比较时起作用:
db.prpackages.aggregate([ {$lookup: {from: "qapackages", localField: "package", foreignField: "package", as: "qacoll"}}, {$match: {"qacoll.version": {$eq: "1"}}} ])
与子阵列的另一个字段进行比较时不起作用:
db.prpackages.aggregate([ {$lookup: {from: "qapackages", localField: "package", foreignField: "package", as: "qacoll"}}, {$match: {"qacoll.version": {$eq: "$version"}}} ])
出了什么问题?