Mongo 聚合和过滤
Mongo Aggregation and Filter
我正在尝试使用以下查询来过滤文档:
db.projects.aggregate([
{
$project: {
deployments: {
$filter: {
input: "$releases.deployments",
as: "deployment",
cond: { $eq: ["$$deployment.environment", "Live"] }
}
}
}
}
])
deployments
的输出始终是一个空数组,尽管如果我将条件更改为 $ne
那么它 return 就是所有结果。
如何将过滤条件设为 return 仅记录 deployment.environment
等于字符串 Live
的记录?
这是 json 的样例:
{
"project_id": "1",
"project_group": "A",
"releases": [
{
"version": "1",
"deployments": [
{
"environment": "Integration",
"created": "2019-10-01T06:40:01.000Z",
"state": "Success",
"name": "Deploy to Integration"
},
{
"environment": "Test",
"created": "2019-10-01T08:23:58.000Z",
"state": "Success",
"name": "Deploy to Test"
},
{
"environment": "Live",
"created": "2019-10-01T09:02:17.000Z",
"state": "Success",
"name": "Deploy to Live"
}
]
}
]
}
如果 releases
是一个嵌入文档但它是一个嵌入文档数组,那么您的查询将完美运行。
以下查询将遍历 releases
的每个元素并过滤环境为 Live
的部署。
db.collection.aggregate([
{
$project:{
"releases":{
$map:{
"input":"$releases",
"as":"release",
"in":{
$mergeObjects:[
"$$release",
{
"deployments":{
$filter:{
"input":"$$release.deployments",
"as":"deployment",
"cond":{
$eq:["$$deployment.environment","Live"]
}
}
}
}
]
}
}
}
}
},
{
$project:{
"releases":{
$filter:{
"input":"$releases",
"as":"release",
"cond":{
$ne:["$$release.deployments.0",null]
}
}
}
}
}
]).pretty()
输出:
{
"_id" : ObjectId("5d93401ef2e6411a68a145ee"),
"releases" : [
{
"version" : "1",
"deployments" : [
{
"environment" : "Live",
"created" : "2019-10-01T09:02:17.000Z",
"state" : "Success",
"name" : "Deploy to Live"
}
]
}
]
}
尝试以下汇总:
db.projects.aggregate([
{
$unwind: "$releases"
},
{
$project: {
version: "$releases.version",
deployments: {
$filter: {
input: "$releases.deployments",
as: "deployment",
cond: {
$eq: [
"$$deployment.environment",
"Live"
]
}
}
}
}
},
{
$group: {
_id: "$_id",
releases: {
$push: {
deployments: "$deployments",
version: "$version"
}
}
}
}
])
我正在尝试使用以下查询来过滤文档:
db.projects.aggregate([
{
$project: {
deployments: {
$filter: {
input: "$releases.deployments",
as: "deployment",
cond: { $eq: ["$$deployment.environment", "Live"] }
}
}
}
}
])
deployments
的输出始终是一个空数组,尽管如果我将条件更改为 $ne
那么它 return 就是所有结果。
如何将过滤条件设为 return 仅记录 deployment.environment
等于字符串 Live
的记录?
这是 json 的样例:
{
"project_id": "1",
"project_group": "A",
"releases": [
{
"version": "1",
"deployments": [
{
"environment": "Integration",
"created": "2019-10-01T06:40:01.000Z",
"state": "Success",
"name": "Deploy to Integration"
},
{
"environment": "Test",
"created": "2019-10-01T08:23:58.000Z",
"state": "Success",
"name": "Deploy to Test"
},
{
"environment": "Live",
"created": "2019-10-01T09:02:17.000Z",
"state": "Success",
"name": "Deploy to Live"
}
]
}
]
}
如果 releases
是一个嵌入文档但它是一个嵌入文档数组,那么您的查询将完美运行。
以下查询将遍历 releases
的每个元素并过滤环境为 Live
的部署。
db.collection.aggregate([
{
$project:{
"releases":{
$map:{
"input":"$releases",
"as":"release",
"in":{
$mergeObjects:[
"$$release",
{
"deployments":{
$filter:{
"input":"$$release.deployments",
"as":"deployment",
"cond":{
$eq:["$$deployment.environment","Live"]
}
}
}
}
]
}
}
}
}
},
{
$project:{
"releases":{
$filter:{
"input":"$releases",
"as":"release",
"cond":{
$ne:["$$release.deployments.0",null]
}
}
}
}
}
]).pretty()
输出:
{
"_id" : ObjectId("5d93401ef2e6411a68a145ee"),
"releases" : [
{
"version" : "1",
"deployments" : [
{
"environment" : "Live",
"created" : "2019-10-01T09:02:17.000Z",
"state" : "Success",
"name" : "Deploy to Live"
}
]
}
]
}
尝试以下汇总:
db.projects.aggregate([
{
$unwind: "$releases"
},
{
$project: {
version: "$releases.version",
deployments: {
$filter: {
input: "$releases.deployments",
as: "deployment",
cond: {
$eq: [
"$$deployment.environment",
"Live"
]
}
}
}
}
},
{
$group: {
_id: "$_id",
releases: {
$push: {
deployments: "$deployments",
version: "$version"
}
}
}
}
])