Home > Net >  Mongo DB aggregation pipeline, get value out of an object that starts with a dollar sign
Mongo DB aggregation pipeline, get value out of an object that starts with a dollar sign

Time:08-08

I'm working on aggregation pipelines for mongo db and got stuck, hoping anyone can help me with it. There is a case where the document looks like this:

{$timestamp: { t: 12121321, i: 1} }

As you can see, the object name starts with a $ sign, my goal is to get a field that contains the value for t, for example:

any_col_name: 12121321

Did you ever encounter this scenario? I'm been trying with $setField/$getField but to no avail.

Thanks in advance!

CodePudding user response:

Query

  • you can use $getField and $literal see also mongodb docs
    ($$ROOT is system variable with value the document)

*maybe there is another way also but this works

Playmongo

aggregate(
[{"$set": 
   {"tValue": 
     {"$getField": 
       {"field": "t",
        "input": 
         {"$getField": 
           {"field": {"$literal": "$timestamp"}, "input": "$$ROOT"}}}}}}])
  • Related