我有3个收藏集:
他们三个人的主键分别存储在<VirtualHost *:80>
ServerName www.phusion.nl
DocumentRoot /websites/phusion/public
<Directory /websites/phusion>
Allow from all
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
#Require all granted
</Directory>
Alias /subapp /websites/secondapp/public
<Location /subapp>
PassengerBaseURI /subapp
PassengerAppRoot /websites/secondapp
PassengerAppType wsgi
PassengerStartupFile passenger_wsgi.py
</Location>
<Directory /websites/secondapp/public>
Allow from all
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
#Require all granted
</Directory>
和
我还有另一个收藏集:_id
,其中存储着
planRecoveryStrategy
planId
businessProcessId
存储在recoveryStrategyId
中的示例文档:
planRecoveryStrategy
给定{ "planId" : "PLN1",
"processId" : "PCS1",
"strategyId" : "RES1"
}
{ "planId" : "PLN1",
"processId" : "PCS2",
"strategyId" : "RES1"
}
{ "planId" : "PLN1",
"processId" : "PCS2",
"strategyId" : "RES2"
}
为'PCS1'时要写的查询是什么
planId
来自{ "planId" : "PLN1",
"processes" : [{
"processId":"PCS1",
"processData":{<data from Business Process Dictionary for PCS1>},
"strategies":[{<data from Recovery Strategy Dictionary for RES1>}]
},
{
"processId":"PCS2",
"processData":{<data from Business Process Dictionary for PCS1>},
"strategies":[{<data from Recovery Strategy Dictionary for RES1>},
{<data from Recovery Strategy Dictionary for RES2>}]
}]
}
?
答案 0 :(得分:1)
尝试对数据进行分组:
db.collection.aggregate([
{
$group: {
_id: {
planId: "$planId",
processId: "$processId"
},
strategies: { $addToSet: "$strategyId" }
}
},
{
$group: {
_id: "$_id.planId",
planId: { $first: "$_id.planId" },
processes: {
$addToSet: {
processId: "$_id.processId",
strategies: "$strategies"
}
}
}
}
]);
排序:
db.collection.aggregate([
{ $sort: { "strategyId": -1 } },
{ $sort: { "processId": 1 } },
{
$group: {
_id: {
planId: "$planId",
processId: "$processId"
},
strategies: { $addToSet: "$strategyId" }
}
},
{
$group: {
_id: "$_id.planId",
planId: { $first: "$_id.planId" },
processes: {
$addToSet: {
processId: "$_id.processId",
strategies: "$strategies"
}
}
}
}
]);
包括其他收藏夹:
db.collection.aggregate([
{ $sort: { "strategyId": -1 } },
{ $sort: { "processId": 1 } },
{
$lookup:
{
from: "BusinessProcess",
localField: "processId",
foreignField: "id",
as: "BusinessProcesses"
}
},
{
$lookup:
{
from: "RecoveryStrategy",
localField: "strategyId",
foreignField: "id",
as: "RecoveryStrategy"
}
},
{
$group: {
_id: {
planId: "$planId",
processId: "$processId",
processData: { $arrayElemAt : ["$BusinessProcesses" , 0] },
},
strategies: { $addToSet: "$RecoveryStrategy" }
}
},
{
$group: {
_id: "$_id.planId",
planId: { $first: "$_id.planId" },
processes: {
$addToSet: {
processId: "$_id.processId",
processData: "$_id.processData",
strategies: "$strategies"
}
}
}
}
]);