2015-09-29 71 views
0

查詢未示出:的MongoDB - 場骨料

db.environments.aggregate(
[{$match:{customer:"CustomerName"}}, 
{$group:{_id:{environment: "$environment", 
cpu_oversubscription:"$cpu_oversubscription", 
memory_oversubscription:"$memory_oversubscription"}}} ]) 

當運行該查詢僅環境和CPU_oversubcription字段被示出,未示出的存儲器領域。

這是輸出我得到:

[{"_id":{"environment":"V&B","cpu_oversubscription":1}} 

當我運行db.environments.find()我看到所有的領域

[{"_id":{"environment":"V&B","cpu_oversubscription":1,"memory_oversubscription":1,"customer":"CustomerName"}} 

什麼是錯我的查詢?

編輯:精確查詢

Environment.aggregate([{$match:{customer:req.query.customer}},{$group:{_id:{environment: "$environment",cpu_oversubscription:"$cpu_oversubscription",memory_oversubscription:"$memory_oversubscription",}}} ]) 

查詢的精確結果

{ "_id" : { "environment" : "1", "cpu_oversubscription" : 1 } } 
{ "_id" : { "environment" : "2", "cpu_oversubscription" : 4 } } 
{ "_id" : { "environment" : "3", "cpu_oversubscription" : 4 } } 
{ "_id" : { "environment" : "4", "cpu_oversubscription" : 4 } } 
{ "_id" : { "environment" : "5", "cpu_oversubscription" : 4 } } 
{ "_id" : { "environment" : "6", "cpu_oversubscription" : 1 } } 
{ "_id" : { "environment" : "7", "cpu_oversubscription" : 1 } } 
{ "_id" : { "environment" : "8", "cpu_oversubscription" : 1 } } 
{ "_id" : { "environment" : "9", "cpu_oversubscription" : 4 } } 
{ "_id" : { "environment" : "10", "cpu_oversubscription" : 4 } } 

db.environments.find的精確結果()(僅第一結果)

{ "_id" : ObjectId("560a5139b56a71ea60890201"), "customer" : "CustomerName", "environment" : "1", "memory_oversubcription" : 1, "cpu_oversubscription" : 1 } 
+0

這裏你的數據樣本是否正確?一切都在'_id'之下,並且你的聚合中沒有任何字段(看起來基於問題,然後由我自己回答)帶有'_id'前綴,如同在{{$ group「:{」_id「:{」environment 「:」$ _id.environment「}}}'。主要指出,這不是前面問題中提出的結構。所以我認爲你在這篇文章中犯了一個錯誤。 –

+0

該查詢是正確的,但我沒有看到它的重點,因爲你最終沒有對任何東西進行分組。如果你不使用任何這些操作符:http://docs.mongodb.org/manual/reference/operator/aggregation/group/#accumulator-operator,一個簡單的查找可以做我認爲的詭計。 –

+0

爲什麼顯示cpu_oversubcription,但memory_oversubscription不? – CMS

回答

0

正如@Yogesh所說,您的數據在您的字段中存在拼寫錯誤。這解決了你的問題。

不過,你不需要爲這個查詢的聚合框架: 一個找到與投影會更高效:

db.environments.find({ "customer" : "CustomerName" }, 
        { _id : 0, "environment" : 1, 
         "memory_oversubscription" : 1, 
         "cpu_oversubscription" : 1 
        }) 

結果將是:

{ 
    "environment" : "1", 
    "memory_oversubscription" : 1, 
    "cpu_oversubscription" : 1 
} 

如果你真的想要在_id字段下的結果出於某種原因,一個$項目會更好:

db.environments.aggregate([ 
    {$match:{"customer" : "CustomerName"}}, 
    {$project: 
    {_id : 
     {"environment" : "$environment", 
     "memory_oversubscription" : "$memory_oversubscription", 
     "cpu_oversubscription" : "$cpu_oversubscription"} 
    } 
    } 
]); 

結果:

{ "_id" : { "environment" : "1", 
      "memory_oversubscription" : 1, 
      "cpu_oversubscription" : 1 } 
} 

沒有道理給我,但也許它爲你:-)。