2016-10-17 5 views
1

を返される結果の数に基づいて別のクエリ条件私はこのような文書いる - 「で」、異なる「詳細」と同じ「タイプ」を同様のスキーマののMongoDB -

{ 
type: "One", 
details: "This is one", 
at: "some place, where one is relevant" 
} 

その他の文書を持つことができますなどがあります。 いくつかのタイプがあります。

ここでは、特定のタイプの文書(limit$inを使用して行うことができます)に一致する特定の数値(上限、たとえば5)を返すためのクエリを記述したいと思います。結果には5つ以下の文書が含まれます。

たとえば、「タイプ」として「1」と「2」のみを許可し、5の制限を使用すると、結果の数が5未満(たとえば2)になると、タイプとして「1」と「2」(つまり2つの文書)と3つの文書があるタイプの文書。

これは意味があると思います!

答えて

1

あなたは私が見ることができるオプションが一致するドキュメントを上に移動して、ソートweightにより、全体の結果を制限するために、インスタンスのための余分なフィールドweightを導入してaggregateを使用しているスクリプトを使用して好きではない場合:

db.test.aggregate({ 
    $project: { 
    type: 1, 
    details: 1, 
    at: 1, 
    weight: { 
     $cond: [ 
     { "$or": [ 
      {$eq: ["$type", "One"] }, 
      {$eq: ["$type", "Two"] } 
     ] }, 
     0, 1] } 
    } }, 
    {$sort: {weight: 1}}, 
    { $limit : 5 } 
); 

この例についての注意。私は$ inを$でいくつかの等価で置き換えます。 weightを最終結果に表示したくない場合は、集計パイプラインで別の投影を適用して削除できます。

テストデシベル:

> db.test.find({}) 
{ "_id" : ObjectId("58067329a7518db4d3d2e973"), "type" : "One", "details" : "This is one", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("5806733da7518db4d3d2e974"), "type" : "Two", "details" : "This is two", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("5806734ba7518db4d3d2e975"), "type" : "Three", "details" : "This is three", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("58067374a7518db4d3d2e976"), "type" : "Four", "details" : "This is four", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("58067381a7518db4d3d2e977"), "type" : "Five", "details" : "This is five", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("5806738fa7518db4d3d2e978"), "type" : "Six", "details" : "This is six", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("580673cfa7518db4d3d2e979"), "type" : "Seven", "details" : "This is seven", "at" : "some place, where one is relevant" }  

結果:

> db.test.aggregate({ $project: { type: 1, details: 1, at: 1, weight: { $cond: [ { "$or": [ {$eq: ["$type", "One"] }, {$eq: ["$type", "Two"] } ] }, 0, 1] } } }, {$sort: {weight: 1}}, { $limit : 5 }, {$project: {type: 1, details: 1, at: 1} }); 
{ "_id" : ObjectId("58067329a7518db4d3d2e973"), "type" : "One", "details" : "This is one", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("5806733da7518db4d3d2e974"), "type" : "Two", "details" : "This is two", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("58067374a7518db4d3d2e976"), "type" : "Four", "details" : "This is four", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("58067381a7518db4d3d2e977"), "type" : "Five", "details" : "This is five", "at" : "some place, where one is relevant" } 
{ "_id" : ObjectId("5806734ba7518db4d3d2e975"), "type" : "Three", "details" : "This is three", "at" : "some place, where one is relevant" }  
+0

パーフェクト@マイク、shauneu!完璧に動作します! 私は、この例では、2つの可能な重みしか持たないことに気づくことはできません。これは条件一致(0)と一致しない(1)と仮定して、それを重みで並べ替えることです。 しかし、私たちはより多くの体重を持つことができますか? $ cond配列に追加する必要があると思います。条件と追加の重み値の両方です。そうですか? –

+0

一致した条件と一致しない結果のドキュメントに含める必要があるため、一致するドキュメントを開始するようにする必要があります。 –