4

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?

1 Answer 1

1

I figured it out. ...

{ $project : {
          ip : "$ip",
          model : "$model",
          cmts : "$cmts",
          prov_group : { '$substr': [$prov, 0, 5] } } } );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.