2017-01-02 8 views
1

をMongoDBをする必要があります -は、ユーザーnodejsごとに1つの文書を取得し、私は以下のような構造で文書を持って

{ 
"_id" : ObjectId("585d64f356ec921620c5d7c5"), 
"createdAt" : ISODate("2016-12-23T17:54:59.193Z"), 
"updatedAt" : ISODate("2016-12-23T17:54:59.193Z"), 
"userid" : ObjectId("5850f42b9a8ea0a43c1355b8"), 
"destination" : { 
    "id" : ObjectId("584fdcf0e6be59514784e9d4"), 
    "lineid" : ObjectId("584f8ddd433c5703acf2618f") 
}, 
"source" : { 
    "id" : ObjectId("584fdda2e6be59514784e9df"), 
    "lineid" : ObjectId("584fdd2fe6be59514784e9d8") 
}, 
"__v" : 0 

}

彼らと同じ構造で、複数の文書であるが、フィールドを「USERID」を繰り返します。 ここでは条件に一致するドキュメントを取得したいが、ユーザーごとに1つのドキュメントしか取得しない。私は "検索"クエリ、 "集約"と "グループ"を使用してみましたが、成功しませんでした。

このクエリを使用してレコードを取得できますが、結果配列に同じユーザーの複数のドキュメントがあります。

Route.find({ 
    $and : [ { 
     $or : [ { 
      'source.lineid' : request.payload.sourcelineid 
     }, { 
      'destination.lineid' : request.payload.destinationlineid 
     }, { 
      'source.id' : request.payload.source 
     }, { 
      'destination.id' : request.payload.destination 
     } ] 
    }, { 
     userid : { 
      $ne : request.user._id 
     } 
    } ] 
}).sort({ 
    createdAt : -1 
}) 
.exec(function(err, sameroutes) { 
    if (err) { 
     reply(err).code(500); 
    } else { 
     reply(sameroutes).code(200); 
    } 
}); 

答えて

0

集計クエリでは、99%正解でしたが、私は逃したことはペイロードからのObjectIDに来るすべてのIDの値を変換しました。だから正解は -

Route.aggregate([ 
    { 
     "$match": { 
      "userid": { "$ne": request.user._id }, 
      "$or": [ 
       { "source.lineid": mongo.ObjectId(request.payload.sourcelineid) }, 
       { "destination.lineid" : mongo.ObjectId(request.payload.destinationlineid) }, 
       { "source.id" : mongo.ObjectId(request.payload.source) }, 
       { "destination.id" : mongo.ObjectId(request.payload.destination) } 
      ] 
     } 
    }, 
    { "$sort": { "userid": 1, "createdAt": -1 } }, 
    { 
     "$group": { 
      "_id": "$userid", 
      "doc_id": { "$first": "$_id" }, 
      "createdAt": { "$first": "$createdAt" }, 
      "updatedAt": { "$first": "$updatedAt" },    
      "destination": { "$first": "$destination" }, 
      "source": { "$first": "$source" } 
     } 
    } 
]).exec(function(err, sameroutes) { 
    if (err) { 
     reply(err).code(500); 
    } else { 
     reply(sameroutes).code(200); 
    } 
}); 
0

ユーザーあたりのことができます。グループ文書aggregate()メソッドを使用して$firstまたは$lastアキュムレータの演算子を使用してフィールドを返します。

フィルタリングは、標準的なMongoDBのクエリを使用$matchを使用して初期 パイプラインステージとして行うことができます。次のパイプラインを実行すると、あなたの望ましい結果を与える必要があります:私は実行しようとしていた

Route.aggregate([ 
    { 
     "$match": { 
      "userid": { "$ne": request.user._id }, 
      "$or": [ 
       { "source.lineid": request.payload.sourcelineid }, 
       { "destination.lineid" : request.payload.destinationlineid }, 
       { "source.id" : request.payload.source }, 
       { "destination.id" : request.payload.destination } 
      ] 
     } 
    }, 
    { "$sort": { "userid": 1, "createdAt": -1 } }, 
    { 
     "$group": { 
      "_id": "$userid", 
      "doc_id": { "$first": "$_id" }, 
      "createdAt": { "$first": "$createdAt" }, 
      "updatedAt": { "$first": "$updatedAt" },    
      "destination": { "$first": "$destination" }, 
      "source": { "$first": "$source" } 
     } 
    } 
]).exec(function(err, sameroutes) { 
    if (err) { 
     reply(err).code(500); 
    } else { 
     reply(sameroutes).code(200); 
    } 
}); 
+0

こんにちは@chridam同じことを試みましたが空の配列を取得しましたが、一致するレコードがデータベースに存在します。 – Rachna

+0

'$ match'演算子だけで集約パイプラインを実行しようとしましたか? – chridam

+0

はい、それは少なくともatleastをチェックすることを考えました。しかし、成功はありません。同じ空の配列。 – Rachna

関連する問題