wannabe programmer
wannabe programmer

Reputation: 683

mongoDB - count common words in 2 strings

Let's say I have some documents, in MongoDB with these sort of info:

{
text: "this is the first this is"
content: "this is this is"
total: 0
}

I would like to count how many times each word in "text" appers in "content", sum all the counts resuls and put in the "total" field.

In the example above:

'this' appears twice in content, 'is': 2, 'the': 0, 'first':0, 'this':2, 'is':2

total: 2+2+0+0+2+2 = 8 so we would like to put 8 in the field 'total'

I know I should do this by iterating through the collection (let's call it 'documents'):

db.documents.find().forEach(
    function(result)
    {
    ... 
    })

not sure what to put inside (i'm new to this, and to JS)

p.s: it's supposed to be case sensitive.

Upvotes: 0

Views: 160

Answers (1)

Valijon
Valijon

Reputation: 13113

Well, take a look JS + MongoDB tutorials. You need to access MongoDB shell or MongoDB GUI (RoboMongo) to execute. You can create new function which counts text in content field (by your rules) and add new field to document.

String.prototype.total = function(content) {
    var result     = 0;
    var contentArr = content.split(" ");
    var textArr    = this.split(" ");
    for(var i = 0; i < textArr.length; i++) {
        for(var j = 0; j < contentArr.length; j++) {
            result += textArr[i] === contentArr[j] ? 1 : 0;
        }
    }
    return result;
}

db.documents.find().forEach(function(doc){
    doc["total"] = doc["text"].total(doc["content"]);
    print(doc);
    // Save again with total value
    //db.documents.save(doc);
})

Result:

{
    "_id" : ObjectId("569c0be30586bcb40f7d253a"),
    "text" : "this is the first this is",
    "content" : "this is this is",
    "total" : 8
}

Upvotes: 1

Related Questions