我得到了一個集合的以下檔案(讓我們命名它myCollection):
{
"_id": {
"$oid": "601a75a0c9a338f09f238816"
},
"Sample": "lie50",
"Chromosome": "chr10",
"Position": {
"$numberLong": "47663"
},
"Reference": "C",
"Mutation": "T",
"Run": "Run_test",
"SYMBOL": "TUBB8"
},
{
"_id": {
"$oid": "601a75a0c9a338f09f238817"
},
"Sample": "lie50",
"Chromosome": "chr10",
"Position": {
"$numberLong": "47876"
},
"Reference": "T",
"Mutation": "C",
"Run": "Run_test",
"SYMBOL": "TUBB8"
},
{
"_id": {
"$oid": "601a75a0c9a338f09f238818"
},
"Sample": "lie50",
"Chromosome": "chr10",
"Position": {
"$numberLong": "48005"
},
"Reference": "G",
"Mutation": "A",
"Run": "Run_test",
"SYMBOL": "TUBB8"
},
{
"_id": {
"$oid": "601a75a0c9a338f09f238819"
},
"Sample": "lie12",
"Chromosome": "chr10",
"Position": {
"$numberLong": "48005"
},
"Reference": "G",
"Mutation": "A",
"Run": "Run_test",
"SYMBOL": "TUBB8"
}
我有興趣列印欄位Chromosome、Position、Reference和中值的不同計數Mutation。這意味著計算以下條目的唯一欄位:
"Chromosome": "chr10", "Position": 47663, "Reference": "C", "Mutation": "T"
"Chromosome": "chr10", "Position": 47876, "Reference": "T", "Mutation": "C"
"Chromosome": "chr10", "Position": 48005, "Reference": "G", "Mutation": "A"
"Chromosome": "chr10", "Position": 48005, "Reference": "G", "Mutation": "A"
這應該是3不同的行。
我已經檢查了多個類似這樣的問題,關于如何列印一個欄位的不同值或使用$unwind/$project。
對于后者,我想為什么不連接 4 個欄位,然后使用lengthwith列印數字$unwind/$project?
我設法做到了這一點:
db.myCollection.aggregate(
[
{
$group:
{
_id: null,
newfield: {
$addToSet:
{
$concat:
[
"$Chromosome",
"_",
{"$toString":"$Position"},
"_",
"$Reference",
"_",
"$Mutation"
]
}
}
}
},
{
$unwind: "$newfield"
},
{
$project: { _id: 0 }
}
]).length
但是,添加.length到此查詢不會回傳任何內容,但不會回傳:
{ "newfield" : "chr10_47663_C_T" }
{ "newfield" : "chr10_47876_T_C" }
{ "newfield" : "chr10_48005_G_A" }
作為參考,我的實際資料包含 20 億份檔案。
uj5u.com熱心網友回復:
欄位應該_id在$groupstage 中傳入,并且還使用$countstage 來獲取總元素而不是回傳所有檔案,
db.myCollection.aggregate([
{
$group: {
_id: {
Chromosome: "$Chromosome",
Position: "$Position",
Reference: "$Reference",
Mutation: "$Mutation"
}
}
},
{ $count: "count" }
])
操場
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/410195.html
標籤:
