2017-08-11 7 views
0

「リンク」コレクションを使用して、さまざまなコレクションのさまざまなレコードをリンクしたいと考えています。後で、特定のオブジェクトについて、それが何をリンクしているかを特定したいが、与えられたオブジェクトrefは含まない。ここで私がこれまで持っているものです。元の一致を除外した集約配列要素

リンク集:

{ "id" : 1, "vertices" : [ { "id" : 1, "type" : "node" }, { "id" : 1, "type" : "entity" } ] } 
{ "id" : 2, "vertices" : [ { "id" : 2, "type" : "node" }, { "id" : 1, "type" : "entity" } ] } 
{ "id" : 3, "vertices" : [ { "id" : 3, "type" : "node" }, { "id" : 2, "type" : "entity" } ] } 
{ "id" : 4, "vertices" : [ { "id" : 1, "type" : "node" }, { "id" : 1, "type" : "alert" } ] } 
{ "id" : 5, "vertices" : [ { "id" : 1, "type" : "node" }, { "id" : 2, "type" : "entity" } ] } 
{ "id" : 6, "vertices" : [ { "id" : 1, "type" : "node" }, { "id": 2, "type": "node" } ] } 

だから私の最初の考えはこれを行うことです。

db.link.aggregate([ 
    {$match:{vertices:{$elemMatch:{id:1,type:"node"}}}}, 
    {$unwind:"$vertices"} 
]); 

{ "_id" : ObjectId("598ccc382381d7587032747c"), "id" : 1, "vertices" : { "id" : 1, "type" : "node" } } 
{ "_id" : ObjectId("598ccc382381d7587032747c"), "id" : 1, "vertices" : { "id" : 1, "type" : "entity" } } 
{ "_id" : ObjectId("598cd0f421d830c187071aca"), "id" : 4, "vertices" : { "id" : 1, "type" : "node" } } 
{ "_id" : ObjectId("598cd0f421d830c187071aca"), "id" : 4, "vertices" : { "id" : 1, "type" : "alert" } } 
{ "_id" : ObjectId("598dd404228b6d88470ed052"), "id" : 5, "vertices" : { "id" : 1, "type" : "node" } } 
{ "_id" : ObjectId("598dd404228b6d88470ed052"), "id" : 5, "vertices" : { "id" : 2, "type" : "entity" } } 
{ "_id" : ObjectId("598e201d720b766ed9f1a496"), "id" : 6, "vertices" : { "id" : 1, "type" : "node" } } 
{ "_id" : ObjectId("598e201d720b766ed9f1a496"), "id" : 6, "vertices" : { "id" : 2, "type" : "node" } } 

をかなり良いです開始しますが、{id:1、type: "node"}の頂点を含む行を取り除きたいと考えています。

それでは、パイプラインに別の$の試合を追加してみましょう。だから私は私だものを

{ "_id" : ObjectId("598ccc382381d7587032747c"), "id" : 1, "vertices" : { "id" : 1, "type" : "entity" } } 
{ "_id" : ObjectId("598cd0f421d830c187071aca"), "id" : 4, "vertices" : { "id" : 1, "type" : "alert" } } 
{ "_id" : ObjectId("598dd404228b6d88470ed052"), "id" : 5, "vertices" : { "id" : 2, "type" : "entity" } } 
{ "_id" : ObjectId("598e201d720b766ed9f1a496"), "id" : 6, "vertices" : { "id" : 2, "type" : "node" } } 

:私は本当に期待

{ "_id" : ObjectId("598dd404228b6d88470ed052"), "id" : 5, "vertices" : { "id" : 2, "type" : "entity" } } 

db.link.aggregate([ 
    {$match:{vertices:{$elemMatch:{id:1,type:"node"}}}}, 
    {$unwind: "$vertices"}, 
    {$match:{ 'vertices.id': {$ne:1}, 'vertices.type': {$ne:'node'} } } 
]); 

これは、結果が得られます2回目の$ match文で間違っていますか?

答えて

0

ロジックが再び襲います!これと闘う他の人のために:

db.link.aggregate([ 
    {$match:{vertices:{$elemMatch:{id:1,type:"node"}}}}, 
    {$unwind: "$vertices"}, 
    {$match:{$or: [ 
     {'vertices.type':{$ne:'node'}}, 
     {'vertices.id':{$ne:1}} 
     ]}} 
]); 

私に望ましい結果が得られます。

関連する問題