如何使用Mongo功能对此数据进行排序:
输入
{ "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" }
{ "_id" : 2, "domainName" : "test2.com", "hosting" : "aws.amazon.com"}
{ "_id" : 3, "domainName" : "test3.com", "hosting" : "aws.amazon.com" }
{ "_id" : 4, "domainName" : "test4.com", "hosting" : "hostgator.com" }
{ "_id" : 5, "domainName" : "test5.com", "hosting" : "aws.amazon.com" }
{ "_id" : 6, "domainName" : "test6.com", "hosting" : "cloud.google.com" }
{ "_id" : 7, "domainName" : "test7.com", "hosting" : "aws.amazon.com" }
{ "_id" : 8, "domainName" : "test8.com", "hosting" : "hostgator.com" }
{ "_id" : 9, "domainName" : "test9.com", "hosting" : "cloud.google.com" }
{ "_id" : 10, "domainName" : "test10.com", "hosting" : "godaddy.com" }
按托管字段顺序获取结果,例如按此顺序:
1)首先 - 所有的教父;
2)第二 - 所有AWS;
3)接下来,其他一切。
输出
{
"result" : [
{
"_id" : 10,
"domainName" : "test10.com",
"hosting" : "godaddy.com"
},
{
"_id" : 2,
"domainName" : "test2.com",
"hosting" : "aws.amazon.com"
},
{
"_id" : 3,
"domainName" : "test3.com",
"hosting" : "aws.amazon.com"
},
{
"_id" : 5,
"domainName" : "test5.com",
"hosting" : "aws.amazon.com"
},
{
"_id" : 7,
"domainName" : "test7.com",
"hosting" : "aws.amazon.com"
},
{
"_id" : 1,
"domainName" : "test1.com",
"hosting" : "hostgator.com"
},
{
"_id" : 4,
"domainName" : "test4.com",
"hosting" : "hostgator.com"
},
{
"_id" : 6,
"domainName" : "test6.com",
"hosting" : "cloud.google.com"
},
{
"_id" : 8,
"domainName" : "test8.com",
"hosting" : "hostgator.com"
},
{
"_id" : 9,
"domainName" : "test9.com",
"hosting" : "cloud.google.com"
},
]
}
通过这个例子,我想以更相关的方式为用户返回结果。在origanl任务中,我想使用其他几个集合对其进行排序,这些集合提供了更多信息。
但如果你帮助我完成上一个任务,那将会很好吗?
UPD:关于问题的第二部分。
另一项任务是如何从一个集合中依赖另一个集合返回可排序数据。
示例:
首先收集之前给出的相同内容:
{ "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" }
...
第二个集合,提供有关托管的其他信息:
{ '_id': 123, 'quality':'best', 'hostings': ["hostgator.com", "aws.amazon.com"]},
{ '_id': 321, 'quality':'good', 'hostings': ["cloud.google.com"]},
{ '_id': 345, 'quality':'bad', 'hostings': ["godaddy.com"]},
结果,我需要按以下顺序从第一个集合数据返回:
1)首先是所有好的主机 2)第二个都好 3)第三个都不好
输出:
{
"result" : [
//Best:
{
"_id" : 1,
"domainName" : "test1.com",
"hosting" : "hostgator.com"
},
{
"_id" : 4,
"domainName" : "test4.com",
"hosting" : "hostgator.com"
},
{
"_id" : 8,
"domainName" : "test8.com",
"hosting" : "hostgator.com"
},
{
"_id" : 2,
"domainName" : "test2.com",
"hosting" : "aws.amazon.com"
},
{
"_id" : 3,
"domainName" : "test3.com",
"hosting" : "aws.amazon.com"
},
{
"_id" : 5,
"domainName" : "test5.com",
"hosting" : "aws.amazon.com"
},
{
"_id" : 7,
"domainName" : "test7.com",
"hosting" : "aws.amazon.com"
},
// Good:
{
"_id" : 9,
"domainName" : "test9.com",
"hosting" : "cloud.google.com"
},
{
"_id" : 6,
"domainName" : "test6.com",
"hosting" : "cloud.google.com"
},
//Bad
{
"_id" : 10,
"domainName" : "test10.com",
"hosting" : "godaddy.com"
}
]
}
更新2
我在上一个例子中得到了很好的答案和例子。非常感谢!但我用另一个例子堆叠。
我需要比较3个集合的ID以按顺序排序 - 第一个:朋友,第二个:'requests',以及:其他用户。
输入
db.friends.find({userId: currentUser});
// {"_id" : "PgC7LrtaZtQsShtzT", "userId" : "tHuxnWxFLHvcpRgHb", "friendId" : "jZagPF7bd4aW8agXb",}
db.requests.find({userId: currentUser});
// looks like friend but with 'requesterId'
现在我需要聚合'用户'集合,定义与前两个集合(朋友,请求)匹配的分数。
使用提供的答案,我管理结果但只有一个集合。如何使用3个或多个?
答案 0 :(得分:1)
您可以将每个hosting
投影到由整数表示的单独类型中,最后对这些整数进行排序。在下面的汇总管道中说明
[
{$lookup: {
from: 'secondCollectionStoringQuality',
localField: 'hosting',
foreignField: 'hostings',
as: 'nw'
}},
{$unwind: '$nw'},
{$project: {
domainName: 1,
hosting: 1,
type: {
$cond: [
{$eq: ['$nw.quality', 'best']},
0,
{$cond: [
{$eq: ['$nw.quality', 'good']},
1,
2
]}
]
}
}},
{$sort: {type: 1}}
]
答案 1 :(得分:0)
该解决方案如何?
1)N个条件。
2)必需的排序顺序可以通过参数传递。
[{
"$lookup": {
"from": "secondCollectionStoringQuality",
"localField": "hosting",
"foreignField": "hostings",
"as": "nw"
}
},
{
"$unwind": "$nw"
},
{
"$project": {
"domainName": 1,
"hosting": 1,
"type": {
"$indexOfArray": [
["best", "good"], "$nw.quality"
]
}
}
},
{
"$project": {
"domainName": 1,
"hosting": 1,
"type": {
"$cond": [ {"$eq":["$type", -1]}, 10000, "$type"]
}
}
},
{
"$sort": {
"type": 1
}
}
]
答案 2 :(得分:0)
如果数据存在于数组中,则上述答案有效。而且您可以在没有$lookup
和$project
的情况下实现这一目标。
.aggregate([
{$addFields: {
sortId: {
$cond: [{$eq: ['$hosting', 'godaddy.com']},0,
{$cond: [{$eq: ['$hosting', 'aws.amazon.com']},1,2]
}
}},
{$sort: {sortId: 1}}
])