mongodb 不同的 groupby 多个键
mongodb distinct of groupby multiple keys
我正在尝试查询 mongoDB 以获取数据聚合(组、匹配、最后)。这是我的文档:
{
"_id" : 1,
"from" : "a",
"to" : "b",
"message" : "a to b",
"createdAt" : ISODate("2015-06-06T16:42:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:42:32.789Z")
}
{
"_id" : 2,
"from" : "a",
"to" : "c",
"message" : "a to c",
"createdAt" : ISODate("2015-06-06T16:43:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:43:32.789Z")
}
{
"_id" : 3,
"from" : "b",
"to" : "c",
"message" : "b to c",
"createdAt" : ISODate("2015-06-06T16:44:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:44:32.789Z")
}
{
"_id" : 4,
"from" : "a",
"to" : "c",
"message" : "a to c2",
"createdAt" : ISODate("2015-06-06T16:45:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:45:32.789Z")
}
{
"_id" : 5,
"from" : "b",
"to" : "c",
"message" : "b to c2",
"createdAt" : ISODate("2015-06-06T16:46:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:46:32.789Z")
}
现在,我想获取一份包含最近的来回组合的文档。示例:
{
"_id" : 1,
"from" : "a",
"to" : "b",
"message" : "a to b",
"createdAt" : ISODate("2015-06-06T16:42:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:42:32.789Z")
}
{
"_id" : 4,
"from" : "a",
"to" : "c",
"message" : "a to c2",
"createdAt" : ISODate("2015-06-06T16:45:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:45:32.789Z")
}
{
"_id" : 5,
"from" : "b",
"to" : "c",
"message" : "b to c2",
"createdAt" : ISODate("2015-06-06T16:46:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:46:32.789Z")
}
我试过这个:
db.collection.aggregate({$match:{$or:[{"from":"552e5d7b62c6a4c67093be5d"},{"to":"552e5d7b62c6a4c67093be5d"}]}})
感谢任何有关代码的帮助。
使用以下聚合管道获得所需结果:
db.collection.aggregate([
{
"$sort": {
"updatedAt": -1
}
},
{
"$group": {
"_id": {
"to": "$to",
"from": "$from"
},
"id": { "$first": "$_id" },
"message": { "$first": "$message" },
"createdAt": { "$first": "$createdAt" },
"updatedAt": { "$first": "$updatedAt" }
}
},
{
"$project": {
"_id" : 0,
"id": 1,
"from" : "$_id.from",
"to": "$_id.to",
"message": 1,
"createdAt": 1,
"updatedAt": 1
}
}
])
我正在尝试查询 mongoDB 以获取数据聚合(组、匹配、最后)。这是我的文档:
{
"_id" : 1,
"from" : "a",
"to" : "b",
"message" : "a to b",
"createdAt" : ISODate("2015-06-06T16:42:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:42:32.789Z")
}
{
"_id" : 2,
"from" : "a",
"to" : "c",
"message" : "a to c",
"createdAt" : ISODate("2015-06-06T16:43:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:43:32.789Z")
}
{
"_id" : 3,
"from" : "b",
"to" : "c",
"message" : "b to c",
"createdAt" : ISODate("2015-06-06T16:44:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:44:32.789Z")
}
{
"_id" : 4,
"from" : "a",
"to" : "c",
"message" : "a to c2",
"createdAt" : ISODate("2015-06-06T16:45:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:45:32.789Z")
}
{
"_id" : 5,
"from" : "b",
"to" : "c",
"message" : "b to c2",
"createdAt" : ISODate("2015-06-06T16:46:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:46:32.789Z")
}
现在,我想获取一份包含最近的来回组合的文档。示例:
{
"_id" : 1,
"from" : "a",
"to" : "b",
"message" : "a to b",
"createdAt" : ISODate("2015-06-06T16:42:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:42:32.789Z")
}
{
"_id" : 4,
"from" : "a",
"to" : "c",
"message" : "a to c2",
"createdAt" : ISODate("2015-06-06T16:45:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:45:32.789Z")
}
{
"_id" : 5,
"from" : "b",
"to" : "c",
"message" : "b to c2",
"createdAt" : ISODate("2015-06-06T16:46:32.789Z"),
"updatedAt" : ISODate("2015-06-06T16:46:32.789Z")
}
我试过这个:
db.collection.aggregate({$match:{$or:[{"from":"552e5d7b62c6a4c67093be5d"},{"to":"552e5d7b62c6a4c67093be5d"}]}})
感谢任何有关代码的帮助。
使用以下聚合管道获得所需结果:
db.collection.aggregate([
{
"$sort": {
"updatedAt": -1
}
},
{
"$group": {
"_id": {
"to": "$to",
"from": "$from"
},
"id": { "$first": "$_id" },
"message": { "$first": "$message" },
"createdAt": { "$first": "$createdAt" },
"updatedAt": { "$first": "$updatedAt" }
}
},
{
"$project": {
"_id" : 0,
"id": 1,
"from" : "$_id.from",
"to": "$_id.to",
"message": 1,
"createdAt": 1,
"updatedAt": 1
}
}
])