2017-01-02 31 views
1

我在下文提到的結構文件: -需要獲取每個用戶一個的NodeJS文件和MongoDB

{ 
"_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 

}

他們都在同一個結構多份文件,但重複「用戶id」字段。 現在我想獲取符合條件但每個用戶只有一個文檔的文檔。我嘗試使用「查找」查詢,「聚合」和「組」,但沒有成功。

我能夠使用此查詢獲取記錄,但結果數組有相同用戶的多個文檔。

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蓄電池運營商領域。

過濾可以做到使用$match它使用標準的MongoDB查詢初始 流水線級。運行下面的管道應該給你想要的結果:

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

是的想法檢查至少。但沒有成功。同一個空陣列。 – Rachna

相關問題