簡體   English   中英

從數組MongoDB獲取唯一ObjectId的計數

[英]Get count of unique ObjectId from array MongoDB

我剛接觸MongoDb並不陌生。 我需要寫一個聚合請求。 這是JSON文檔結構。

{ 
    "_id" : ObjectId("5a72f7a75ef7d430e8c462d2"), 
    "crawler_id" : ObjectId("5a71cbb746e0fb0007adc6c2"), 
    "skill" : "stack", 
    "created_date" : ISODate("2018-02-01T13:19:03.522+0000"), 
    "modified_date" : ISODate("2018-02-01T13:22:23.078+0000"), 
    "connects" : [
        {
            "subskill" : "we’re", 
            "weight" : NumberInt(1), 
            "parser_id" : [
                ObjectId("5a71d88d5ef7d41964fbec11")
            ]
        }, 
        {
            "subskill" : "b1", 
            "weight" : NumberInt(2), 
            "parser_id" : [
                ObjectId("5a71d88d5ef7d41964fbec11"), 
                ObjectId("5a71d88d5ef7d41964fbec1b")
            ]
        }, 
        {
            "subskill" : "making", 
            "weight" : NumberInt(2), 
            "parser_id" : [
                ObjectId("5a71d88d5ef7d41964fbec1b"), 
                ObjectId("5a71d88d5ef7d41964fbec1c")
            ]
        }, 
        {
            "subskill" : "delivery", 
            "weight" : NumberInt(2), 
            "parser_id" : [
                ObjectId("5a71d88d5ef7d41964fbec1c"), 
                ObjectId("5a71d88d5ef7d41964fbec1e")
            ]
        }
    ]
}

我需要結果返回技能名稱和唯一parser_id的數量。 在這種情況下,結果應為:

[
   {
    "skill": "stack",
    "quantity": 4
    }
]

其中“堆棧”-技能名稱,“數量”-唯一parser_id的計數。

ObjectId("5a71d88d5ef7d41964fbec11")
ObjectId("5a71d88d5ef7d41964fbec1b")
ObjectId("5a71d88d5ef7d41964fbec1c")
ObjectId("5a71d88d5ef7d41964fbec1e")

有人可以幫我這個要求嗎?

給定您問題中提供的文檔,此命令...

db.collection.aggregate([
    { $unwind: "$connects" },

    // count all occurrences
    { "$group": { "_id": {skill: "$skill", parser_id: "$connects.parser_id"}, "count": { "$sum": 1 } }},

    // sum all occurrences and count distinct
    { "$group": { "_id": "$_id.skill", "quantity": { "$sum": 1 } }},

    // (optional) rename the '_id' attribute to 'skill'
    { $project: { 'skill': '$_id', 'quantity': 1, _id: 0 } }
])

... 將返回:

{
    "quantity" : 4,
    "skill" : "stack"
}

上面的命令按skillconnects.parser_id分組,然后獲得這些組的不同計數。

您的命令包含java標記,因此我懷疑您正在尋找使用MongoDB Java驅動程序執行同一命令的方法。 下面的代碼(使用MongoDB Java驅動程序v3.x)將返回相同的結果:

MongoClient mongoClient = ...;

MongoCollection<Document> collection = mongoClient.getDatabase("...").getCollection("...");

List<Document> documents = collection.aggregate(Arrays.asList(
        Aggregates.unwind("$connects"),
        new Document("$group", new Document("_id", new Document("skill", "$skill").append("parser_id", "$connects.parser_id"))
                .append("count", new Document("$sum", 1))),
        new Document("$group", new Document("_id", "$_id.skill").append("quantity", new Document("$sum", 1))),
        new Document("$project", new Document("skill", "$_id").append("quantity", 1).append("_id", 0))
)).into(new ArrayList<>());

for (Document document : documents) {
    logger.info("{}", document.toJson());
}

注意:此代碼故意使用形式為new Document(<pipeline aggregator>, ...)而不是Aggregators實用程序,以便更輕松地查看 shell命令與其等效的Java之間的轉換。

嘗試$project$reduce

$setUnion用於僅保留不同的ID,最后$size用於獲取不同的數組計數

db.col.aggregate(
    [
        {$project : {
                _id : 0,
                skill : 1,
                quantity : {$size :{$reduce : {input : "$connects.parser_id", initialValue : [] , in : {$setUnion : ["$$value", "$$this"]}}}}
            }
        }
    ]
).pretty()

結果

{ "skill" : "stack", "quantity" : 4 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM