Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a javascript loop that does a particular task of looping over a javascript hash and calling aggregate over my collection.

I was able to insert a variable into the aggregate query for the $match function, but I cannot for the $project function.

I want to match across the hash keys and then display the hash values in conjunction with the values already in the document that I just matched. These hash values and label were not in the document beforehand.

var cmtss = {};

for (var item in cursor['result'])
{
    var prov = cursor['result'][item]['prov_group'];
    cmtss[cursor['result'][item]['name']] = prov;
}

for (var item in cmtss)
{
    var cmts = "$" + cmtss[item];

    result = db.modems.aggregate( { $match : { cmts: item } } ,
                                  { $project : {
                                       ip : "$ip",
                                       model : "$model",
                                       cmts : "$cmts",
                                       prov_group : cmts } } );
    printjson(result);
}

As you can see, I include a $match, where I want the cmts field to match to the key provided. But with those matches I want to display the 3 fields including a brand new field that I add in but its value is the hash value from cmtss. I tried with and without the $ operator before the value. It is simply not displaying a prov_group at all in the result documents.

Do I need to use $add somehow?

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

I figured it out. ...

{ $project : {
          ip : "$ip",
          model : "$model",
          cmts : "$cmts",
          prov_group : { '$substr': [$prov, 0, 5] } } } );
share
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.