如何通过节点js

时间:2017-11-24 21:33:02

标签: javascript node.js mongodb

查看我的 imageDetails 集合的文档:

> db.imageDetails.find()
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e61"), "keyword" : "sachin", "name" : "sachin_1511554882309_1.jpg", "fullpath" : "Download/sachin_1511554882309_1.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e62"), "keyword" : "sachin", "name" : "sachin_1511554882317_2.jpg", "fullpath" : "Download/sachin_1511554882317_2.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e63"), "keyword" : "sachin", "name" : "sachin_1511554882319_3.jpg", "fullpath" : "Download/sachin_1511554882319_3.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e64"), "keyword" : "sachin", "name" : "sachin_1511554882319_4.jpg", "fullpath" : "Download/sachin_1511554882319_4.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e65"), "keyword" : "sachin", "name" : "sachin_1511554882320_5.jpg", "fullpath" : "Download/sachin_1511554882320_5.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e66"), "keyword" : "sachin", "name" : "sachin_1511554882320_6.jpg", "fullpath" : "Download/sachin_1511554882320_6.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e67"), "keyword" : "sachin", "name" : "sachin_1511554882320_7.jpg", "fullpath" : "Download/sachin_1511554882320_7.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e68"), "keyword" : "sachin", "name" : "sachin_1511554882320_8.jpg", "fullpath" : "Download/sachin_1511554882320_8.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e69"), "keyword" : "sachin", "name" : "sachin_1511554882320_9.jpg", "fullpath" : "Download/sachin_1511554882320_9.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6a"), "keyword" : "sachin", "name" : "sachin_1511554882320_10.jpg", "fullpath" : "Download/sachin_1511554882320_10.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6b"), "keyword" : "sachin", "name" : "sachin_1511554882321_11.jpg", "fullpath" : "Download/sachin_1511554882321_11.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6c"), "keyword" : "sachin", "name" : "sachin_1511554882321_12.jpg", "fullpath" : "Download/sachin_1511554882321_12.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6d"), "keyword" : "sachin", "name" : "sachin_1511554882321_13.jpg", "fullpath" : "Download/sachin_1511554882321_13.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6e"), "keyword" : "sachin", "name" : "sachin_1511554882321_14.jpg", "fullpath" : "Download/sachin_1511554882321_14.jpg" }
{ "_id" : ObjectId("5a187f4f2d4b2817b8448e6f"), "keyword" : "sachin", "name" : "sachin_1511554882322_15.jpg", "fullpath" : "Download/sachin_1511554882322_15.jpg" }
>

我希望nodejs中字段&#39; <&#39; 的不同值作为mongodb中的替换值,如:

> db.imageDetails.distinct('keyword');
[ "sachin" ]

我试过但在节点js mongodb模块中没有找到任何有效的方法,所以如果有聚合选项或更有效的东西,请建议任何方法。我被卡住了:

MongoClient.connect(url, function(err, db) {
    if (err)
    throw err;
    var cursor = db.collection(collectionName).find(findQuery);
    cursor.each(function(err, doc)

1 个答案:

答案 0 :(得分:1)

您可以在收藏中致电Radix。这是一个例子:

db.collection(collectionName).distinct('keyword', function(err, docs) {
  // ...
});

请注意,distinct不会返回distinct。您可以使用我上面显示的回调方法或使用Cursor,这可以通过不提供回调来实现。 e.g:

db.collection(collectionName).distinct('keyword')
    .then(docs => {
        // ...
    });