2017-01-02 54 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

相关问题