2017-07-26 2 views
0

私はmongooseを使用しています。私はこの1つのコレクションのデータをサブドキュメントとして取得する別のドキュメントの中にmongoDb

{ 
    "_id" : ObjectId("597568f5cf12434674"), 
    "data": "test data", 
    "createBy" : "testing", 
    "modifyBy" : "testing", 
    "modifyDate" : ISODate("2017-07-24T03:26:45.350Z"), 
    "createDate" : ISODate("2017-07-24T03:26:45.350Z"), 
} 

ようmetaコレクションを持って、この_idは私が注文の詳細を得るとき、私は私の最終的な結果は次のようになりように、2つのコレクションに参加する必要がありorders

{ 
    "_id" : ObjectId("597818bf5332f91213411"), 
    "type" : "food order", 
    "isActive" : true, 
    "files" : [ 
     { 
      "metaId" : "597568f5cf12434674", 
      "_id" : ObjectId("597818sdfasdaf2222") 
     } 
    ] 
} 

metaIdと呼ばれる別のコレクションでありますこのように

{ 
    "_id" : ObjectId("597818bf5332f91213411"), 
    "type" : "food order", 
    "isActive" : true, 
    "files" : [ 
     { 
      "metaId" : "597568f5cf12434674", 
      "data": "test data", 
      "createBy" : "testing", 
      "modifyBy" : "testing", 
      "modifyDate" : ISODate("2017-07-24T03:26:45.350Z"), 
      "createDate" : ISODate("2017-07-24T03:26:45.350Z"), 
      "_id" : ObjectId("597818sdfasdaf2222") 
     } 
    ] 
} 

私はこのクエリを書いていますが、空を返します。これはinc私はメタデータをオーダーの中に入れたいからです。

const query = db.orders.aggregate([ 
     {'$match': {'_id': _id}}, 
     {'$lookup': { 
     'localField': 'files.metaId', 
     'from': 'meta', 
     'foreignField': '_id', 
     'as': 'metaInfo' 
     }}, 
     {'$unwind': '$metaInfo'}, 
     {'$project': { 
     'type': 1, 
     'isActive': 1, 
     'metaInfo.data': 1 
     }} 
    ]); 

私はこのmongoをやってもらえますか、私はmongodbにはとても新しいです。事前に感謝してください、助けてください。あなたが最初にそれをほどくする必要がありますので

答えて

1

$lookup集約演算子は「側から」上のアレイでは動作しません。

{ 
    "_id" : ObjectId("59784eb56149554af36a5666"), 
    "type" : "food order", 
    "isActive" : true, 
    "files" : [ 
     { 
      "_id" : ObjectId("59784e706149554af36a5621"), 
      "data" : "test data", 
      "createBy" : "testing", 
      "modifyBy" : "testing", 
      "modifyDate" : ISODate("2017-07-24T03:26:45.350Z"), 
      "createDate" : ISODate("2017-07-24T03:26:45.350Z") 
     } 
    ] 
} 

db.getCollection('orders').aggregate([ 
    // matches a single document in the orders collection 
    {'$match': {'_id': ObjectId("59784eb56149554af36a5666")}}, 

    // unwinds orders.files so that it is no longer an array 
    {'$unwind': '$files'}, 

    // performs the lookup on the meta collection and assigns the 'looked up' sub document to the attribute: "files" 
    {'$lookup': { 
    'localField': 'files.metaId', 
    'from': 'meta', 
    'foreignField': '_id', 
    'as': 'files' 
    }} 
]); 

上記のコマンドを次のドキュメントを返します。

注:これは、files.metaIdを失うので、これは、orders.files.metaIdからに参加したので、ファイルの属性にメタコレクションから '参照された'コンテンツ文書を読み込みますファイルサブ文書の_idの値は、orders.files.metaIdと同じです。あなたは少しのコマンドを変更した場合、これを見ることができます:

db.getCollection('orders').aggregate([ 
    // matches a single document in the orders collection 
    {'$match': {'_id': ObjectId("59784eb56149554af36a5666")}}, 

    // unwinds orders.files so that it is no longer an array 
    {'$unwind': '$files'}, 

    // performs the lookup on the meta collection and assigns the 'looked up' sub document to the attribute: "files" 
    {'$lookup': { 
     'localField': 'files.metaId', 
     'from': 'meta', 
     'foreignField': '_id', 
     'as': 'metaData' 
    }} 
    ]); 

これが返されます。files.metaId == metaData._id

{ 
    "_id" : ObjectId("59784eb56149554af36a5666"), 
    "type" : "food order", 
    "isActive" : true, 
    "files" : { 
     "metaId" : ObjectId("59784e706149554af36a5621") 
    }, 
    "metaData" : [ 
     { 
      "_id" : ObjectId("59784e706149554af36a5621"), 
      "data" : "test data", 
      "createBy" : "testing", 
      "modifyBy" : "testing", 
      "modifyDate" : ISODate("2017-07-24T03:26:45.350Z"), 
      "createDate" : ISODate("2017-07-24T03:26:45.350Z") 
     } 
    ] 
} 

を。

+0

これはうまくいきます:)どうもありがとうございました 。私たちはmetaIdの値を削除するのではなく、古い_idのフィールドを保持する必要があります –

+0

私はこのポイントに対処するための答えを更新しました: '' metaId '"の値をそれを取り除く。しかし、私はこの要求にはっきりしていません。「古いフィールドを保持する必要があります。 – glytching