这是mongo db中的JSON对象:
{
"_id": ObjectId("5a1e50efcb4ecdfe9b41d097"),
"ProjectId": NumberInt(1),
"TicketId": NumberInt(64),
"Artifacts": [
{
"Slug": ObjectId("5a1e50e6a3d5592b583ec566"),
"UploadedOn": ISODate("2017-11-29T06:17:10.0Z"),
"UploadedBy": NumberInt(6),
"Status": NumberInt(1),
"ArtifactType": "image",
"isThumbnailExist": NumberInt(0),
"ThumbnailPath": "/files/story/thumbnails",
"FileName": "7b179efffd03e92e495b802c24d3f357-B1",
"OriginalFileName": "B1.png",
"Extension": "png"
},
{
"Slug": ObjectId("5a1e515aa3d5592b583ec56a"),
"UploadedOn": ISODate("2017-11-29T06:19:06.0Z"),
"UploadedBy": NumberInt(6),
"Status": NumberInt(0),
"ArtifactType": "image",
"isThumbnailExist": NumberInt(0),
"ThumbnailPath": "/files/story/thumbnails",
"FileName": "b83f3aa5d741fb18f1d527757cc002d2-B2",
"OriginalFileName": "B2.png",
"Extension": "png"
}
]
}
我只想检索 Artifacts.Status为1 的记录。!
我该怎么做。我不熟悉SQL。
这是我的mongo查询功能:
public static function getACtiveArtifacts($projectId, $ticketId) {
try{
$query = new Query();
$query->from('TicketArtifacts')
->select(array("Artifacts"))
->where(['TicketId' => (int) $ticketId, "ProjectId" => (int) $projectId]);
$ticketArtifactsDetails = $query->one();
return $ticketArtifactsDetails;
} catch (\Throwable $ex) {
Yii::error("TicketArtifactsCollection:getTicketArtifacts::" . $ex->getMessage() . "--" . $ex->getTraceAsString(), 'application');
throw new ErrorException($ex->getMessage());
}
}
我的预期结果:(因为"状态":NumberInt(1))
{
"_id": ObjectId("5a1e50efcb4ecdfe9b41d097"),
"ProjectId": NumberInt(1),
"TicketId": NumberInt(64),
"Artifacts": [
{
"Slug": ObjectId("5a1e50e6a3d5592b583ec566"),
"UploadedOn": ISODate("2017-11-29T06:17:10.0Z"),
"UploadedBy": NumberInt(6),
"Status": NumberInt(1),
"ArtifactType": "image",
"isThumbnailExist": NumberInt(0),
"ThumbnailPath": "/files/story/thumbnails",
"FileName": "7b179efffd03e92e495b802c24d3f357-B1",
"OriginalFileName": "B1.png",
"Extension": "png"
}
]
}
答案 0 :(得分:1)
这可以使用聚合管道完成,使用$unwind和$match来实现结果。
Mongo Shell查询
db.collection.aggregate([
{$unwind:"$Artifacts"},
{$match:{"Artifacts.Status":1}}
])
当我们在我们的集合中应用$ unwind聚合管道来展开将导致文档的工件时,如下所示
{
"_id" : ObjectId("5a1e50efcb4ecdfe9b41d097"),
"ProjectId" : 1,
"TicketId" : 64,
"Artifacts" : {
"Slug" : ObjectId("5a1e50e6a3d5592b583ec566"),
"UploadedOn" : ISODate("2017-11-29T06:17:10Z"),
"UploadedBy" : 6,
"Status" : 1,
"ArtifactType" : "image",
"isThumbnailExist" : 0,
"ThumbnailPath" : "/files/story/thumbnails",
"FileName" : "7b179efffd03e92e495b802c24d3f357-B1",
"OriginalFileName" : "B1.png",
"Extension" : "png"
}
}
{
"_id" : ObjectId("5a1e50efcb4ecdfe9b41d097"),
"ProjectId" : 1,
"TicketId" : 64,
"Artifacts" : {
"Slug" : ObjectId("5a1e515aa3d5592b583ec56a"),
"UploadedOn" : ISODate("2017-11-29T06:19:06Z"),
"UploadedBy" : 6,
"Status" : 0,
"ArtifactType" : "image",
"isThumbnailExist" : 0,
"ThumbnailPath" : "/files/story/thumbnails",
"FileName" : "b83f3aa5d741fb18f1d527757cc002d2-B2",
"OriginalFileName" : "B2.png",
"Extension" : "png"
}
}
然后通过将条件Artifacts.Status:1
应用$ match,我们将获得最终结果。
{
"_id" : ObjectId("5a1e50efcb4ecdfe9b41d097"),
"ProjectId" : 1,
"TicketId" : 64,
"Artifacts" : {
"Slug" : ObjectId("5a1e50e6a3d5592b583ec566"),
"UploadedOn" : ISODate("2017-11-29T06:17:10Z"),
"UploadedBy" : 6,
"Status" : 1,
"ArtifactType" : "image",
"isThumbnailExist" : 0,
"ThumbnailPath" : "/files/story/thumbnails",
"FileName" : "7b179efffd03e92e495b802c24d3f357-B1",
"OriginalFileName" : "B1.png",
"Extension" : "png"
}
}
答案 1 :(得分:1)
您可以使用$redact聚合阶段。
MongoDB聚合查询:
db.collection.aggregate([
{
$match: {"ProjectId":1}
},
{
$redact: {
$cond: {
if: { $eq: ["$Status", 1] },
then: "$$PRUNE",
else: "$$DESCEND"
}
}
}
])
P.S。抱歉,我没有使用Yii框架编写代码,因此我无法向您提供最终代码。