Mongodb 检查 collection 相同的参数
Mongodb check collection for same parameters
我正在尝试检查 collection 是否有其他记录具有与 mongodb 聚合相同的参数。示例:
汽车collection:
[
{
"year": "2020",
"model": "Opel"
}, {
"year": "2020",
"model": "Renault"
}, {
"year": "2019",
"model": "Renault"
},
]
获取:按年份获取记录return:
[
{
"year": "2020",
"model": "Opel"
}, {
"year": "2020",
"model": "Renault"
},
]
为每条记录添加变量 - "Does current record with name {name} has another record in this table - true / false"。
预期结果:
[
{
"year": "2020",
"model": "Opel",
"hasAnotherRecord": false
}, {
"year": "2020",
"model": "Renault",
"hasAnotherRecord": true
}
]
我怎样才能做到这一点?
不确定此管道的效率如何,但它 returns 您的预期输出:
db.cars.aggregate(
[
{
"$group": {
"_id": "$model",
"cars": { "$push": { "model": "$model", "year": "$year" }}}
},
{
"$project": {
"cars": {
"$filter": {
"input": "$cars",
"as": "c",
"cond": { "$eq": [ "$$c.year", "2020" ]
}
}
},
"hasAnotherRecord": {
"$gt": [ { "$size": "$cars" }, 1 ]
},
"_id": 0
}
},
{
"$match": {
"cars": {
"$ne": null,
"$not": { "$size": 0 }
}
}
},
{
"$project": {
"model": {
"$arrayElemAt": [ "$cars.model", 0 ]
},
"year": {
"$arrayElemAt": [ "$cars.year", 0 ]
},
"_id": 0
}
}
]
)
c#测试程序:
using MongoDB.Entities;
using MongoDB.Entities.Core;
using System.Linq;
namespace Whosebug
{
public class car : Entity
{
public string year { get; set; }
public string model { get; set; }
}
public static class Program
{
private static void Main()
{
new DB("test");
new[] {
new car { model= "opel", year= "2020"},
new car { model= "renault", year= "2020"},
new car { model= "renault", year= "2019"},
new car { model= "nissan", year= "2021"}
}.Save();
var result = DB.Queryable<car>()
.GroupBy(c => c.model,
(model, cars) => new
{
model,
cars = cars.Select(c => new car
{
model = c.model,
year = c.year
})
})
.Select(x => new
{
cars = x.cars.Where(c => c.year == "2020"),
hasAnotherRecord = x.cars.Count() > 1
})
.Where(x => x.cars.Any())
.Select(x => new
{
x.cars.First().model,
x.cars.First().year
})
.ToList();
}
}
}
我正在尝试检查 collection 是否有其他记录具有与 mongodb 聚合相同的参数。示例:
汽车collection:
[
{
"year": "2020",
"model": "Opel"
}, {
"year": "2020",
"model": "Renault"
}, {
"year": "2019",
"model": "Renault"
},
]
获取:按年份获取记录return:
[
{
"year": "2020",
"model": "Opel"
}, {
"year": "2020",
"model": "Renault"
},
]
为每条记录添加变量 - "Does current record with name {name} has another record in this table - true / false"。 预期结果:
[
{
"year": "2020",
"model": "Opel",
"hasAnotherRecord": false
}, {
"year": "2020",
"model": "Renault",
"hasAnotherRecord": true
}
]
我怎样才能做到这一点?
不确定此管道的效率如何,但它 returns 您的预期输出:
db.cars.aggregate(
[
{
"$group": {
"_id": "$model",
"cars": { "$push": { "model": "$model", "year": "$year" }}}
},
{
"$project": {
"cars": {
"$filter": {
"input": "$cars",
"as": "c",
"cond": { "$eq": [ "$$c.year", "2020" ]
}
}
},
"hasAnotherRecord": {
"$gt": [ { "$size": "$cars" }, 1 ]
},
"_id": 0
}
},
{
"$match": {
"cars": {
"$ne": null,
"$not": { "$size": 0 }
}
}
},
{
"$project": {
"model": {
"$arrayElemAt": [ "$cars.model", 0 ]
},
"year": {
"$arrayElemAt": [ "$cars.year", 0 ]
},
"_id": 0
}
}
]
)
c#测试程序:
using MongoDB.Entities;
using MongoDB.Entities.Core;
using System.Linq;
namespace Whosebug
{
public class car : Entity
{
public string year { get; set; }
public string model { get; set; }
}
public static class Program
{
private static void Main()
{
new DB("test");
new[] {
new car { model= "opel", year= "2020"},
new car { model= "renault", year= "2020"},
new car { model= "renault", year= "2019"},
new car { model= "nissan", year= "2021"}
}.Save();
var result = DB.Queryable<car>()
.GroupBy(c => c.model,
(model, cars) => new
{
model,
cars = cars.Select(c => new car
{
model = c.model,
year = c.year
})
})
.Select(x => new
{
cars = x.cars.Where(c => c.year == "2020"),
hasAnotherRecord = x.cars.Count() > 1
})
.Where(x => x.cars.Any())
.Select(x => new
{
x.cars.First().model,
x.cars.First().year
})
.ToList();
}
}
}