查询:mongoDb

时间:2015-10-14 05:45:37

标签: json mongodb

假设我的JSON如下:

{ "id":0,"keywords":"amount,debited,account,ticket,not,generated,now" }
{ "id":1,"keywords":"how,safe,gocash" }
{ "id":2,"keywords":"how,referral,program,gocash,works" }

如果我的数组就像

array =["how","safe","gocash"];

然后我如何得到第一次检查时的计数; count应该为零,第二个为三,第二个为第二个。 (这意味着字符串中存在多少个数组元素)

我应该采用什么方法或采用什么方法?

1 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是,需要对模式进行某种形式的修改,方法是在数组中添加一个包含keywords的额外字段。运行聚合管道以返回与原始字符串匹配的所需元素数时,此字段变得非常方便。

要添加其他字段,您需要 Bulk API 操作来更新集合,如下所示:

var bulk = db.collection.initializeOrderedBulkOp(),   
    count = 0;

db.collection.find({"keywords": { "$exists": true, "$type": 2 }}).forEach(function(doc) { 
    var keywordsArray = doc.keywords.split(',');
    bulk.find({ "_id": doc._id }).updateOne({ 
        "$set": { "keywordsArray": keywordsArray }
    });
    count++;
    if (count % 100 == 0) {
        bulk.execute();
        bulk = db.collection.initializeUnorderedBulkOp();
    }
});

if (count % 100 != 0) { bulk.execute(); }

上面创建了一个额外的字段"keywordsArray",它是将keywords字符串拆分为数组的结果。

操作完成后,您的样本集将包含以下文档:

/* 0 */
{
    "_id" : ObjectId("561e24e9ba53a16c763eaab4"),
    "id" : 0,
    "keywords" : "amount,debited,account,ticket,not,generated,now",
    "keywordsArray" : [ 
        "amount", 
        "debited", 
        "account", 
        "ticket", 
        "not", 
        "generated", 
        "now"
    ]
}

/* 1 */
{
    "_id" : ObjectId("561e24e9ba53a16c763eaab5"),
    "id" : 1,
    "keywords" : "how,safe,gocash",
    "keywordsArray" : [ 
        "how", 
        "safe", 
        "gocash"
    ]
}

/* 2 */
{
    "_id" : ObjectId("561e24e9ba53a16c763eaab6"),
    "id" : 2,
    "keywords" : "how,referral,program,gocash,works",
    "keywordsArray" : [ 
        "how", 
        "referral", 
        "program", 
        "gocash", 
        "works"
    ]
}

进入下一阶段,聚合框架管道,运行以下管道操作,该操作使用 $let $size $setIntersection 运算符计算出所需的计数结果:

var array = ["how","safe","gocash"];
db.collection.aggregate([
    {
        "$project": {
            "id": 1, "keywords": 1,
            "count": {
                "$let": {
                   "vars": {
                      "commonToBoth": { "$setIntersection": [ "$keywordsArray", array ] }
                   },
                   "in": { "$size": "$$commonToBoth" }
                }
            }
        }
    }
])

示例输出:

/* 0 */
{
    "result" : [ 
        {
            "_id" : ObjectId("561e24e9ba53a16c763eaab4"),
            "id" : 0,
            "keywords" : "amount,debited,account,ticket,not,generated,now",
            "count" : 0
        }, 
        {
            "_id" : ObjectId("561e24e9ba53a16c763eaab5"),
            "id" : 1,
            "keywords" : "how,safe,gocash",
            "count" : 3
        }, 
        {
            "_id" : ObjectId("561e24e9ba53a16c763eaab6"),
            "id" : 2,
            "keywords" : "how,referral,program,gocash,works",
            "count" : 2
        }
    ],
    "ok" : 1
}