我有一個集合,其中包含兩種類型的文檔。基於我想要執行查找聚合的文檔類型。只有當本地域存在時才執行條件查找?
Action是一個集合,它包含對話和消息類型的動作。我想根據MongoDB中的字段對對話和消息進行不同的查找。
談話式的操作文件是如下的消息
{
"_id" : ObjectId("592bdeaf45c7201421793871"),
"actionType" : "message",
"created" : NumberInt(1496047280),
"messageId" : ObjectId("592bdeaf45c7201421793870")
}
我希望有一個特定的時間之後的行動和要執行的查找來獲得對話和消息數據
{
"_id" : ObjectId("592bdeaf45c7201421793871"),
"actionType" : "conversation",
"created" : NumberInt(1496047280),
"users" : [
ObjectId("590c53a85fba594a59fe3d0f"),
ObjectId("590c50175df715499129e41b")
],
"conversationId" : ObjectId("592bdeaf45c7201421793870"),
"missionId" : ObjectId("590c50fa5df715499129e41c")
}
行動文件從他們的對象ID。
我想這
let matchQuery = {
users : new ObjectId(userId),
created:
{
$gt : createdTime
}};
let aggregation = [
{
$match : matchQuery
},
{$cond :
{if:
{
$users:{
$exists:true
}
},
then: {
$unwind : "$users",
$lookup: {
from : "users",
localfield:"$users",
foreignfield:"_id",
as:"userDetail"
},
$group:{
"users" : { $push : "userDetail"}
}
}
}
},
{$cond :
{if:
{
$conversationId:{
$exists:true
}
},
then: {
$lookup : {
from:"conversations",
localfield:"conversationId",
foreignfield:"_id",
as:"conversationDetail"
}
}
}
},
{$cond :
{if:
{
$missionId:{
$exists:true
}
},
then: {
$lookup : {
from:"services_v2",
localfield:"missionId",
foreignfield:"_id",
as:"missionDetail"
}
}
}
},
{$cond :
{if:
{
$messageId:{
$exists:true
}
},
then: {
$lookup : {
from:"messagev2",
localfield:"messageId",
foreignfield:"_id",
as:"messageDetail"
}
}
}
},
{
$project : {
"_id" : 1,
"actionType" : 1,
"userDetail":1,
"conversationDetail":1,
"missionDetail":1,
"messageDetail":1
}}
];
connection.collection('actions')
.aggregate(aggregation).toArray((err,result)=> {
if(err){
console.log(err);
}
console.log(result);
})
};
我試過了開關,但它不能識別查找或展開表達式。 –
在你的問題中顯示你正在談論的內容。使用一些真實的文件並顯示預期的結果。在你的問題上使用[編輯](https://stackoverflow.com/posts/44241188/edit)鏈接,而不是添加評論。 –
用代碼更新了它。 –