MongoDB Query To Compare timestamp with nested object timestamp

185 Views Asked by At

I have documents of the below structure:

    {
       "_id":{
          "$oid":"6145b4405df7711fe1e2f3e0"
       },
       "name":"Testing1",
       "description":"Testing Project1",
       "created_at":{
          "$date":"2021-09-18T09:41:20.985Z"
       },
       "updated_at":{
          "$date":"2021-09-18T09:41:20.985Z"
       },
       "experiments":null,
       "contributors":[
          "local"
       ],
       "tags":[
          "dev",
          "prod"
       ],
       "owner":"localhost",
       "tokens":[
          {
             "owner":"localhost",
             "created_at":{
                "$date":"2021-09-18T16:38:02.911Z"
             },
             "token":"testToken"
          }
       ]
    }
############
{
   "_id":{
      "$oid":"6145b4405df7711fe1e2f3e0"
   },
   "name":"Testing2",
   "description":"Testing Project2",
   "created_at":{
      "$date":"2021-09-18T09:41:20.985Z"
   },
   "updated_at":{
      "$date":"2021-09-18T09:41:20.985Z"
   },
   "experiments":null,
   "contributors":[
      "local"
   ],
   "tags":[
      "dev",
      "prod"
   ],
   "owner":"localhost",
   "tokens":[
      {
         "owner":"root",
         "created_at":{
            "$date":"2021-09-18T16:38:02.911Z"
         },
         "token":"rootToken"
      },
      {
         "owner":"localhost",
         "created_at":{
            "$date":"2021-09-18T16:38:02.911Z"
         },
         "token":"localToken"
      }

   ]
}
  1. How to return only token and created_at (not the complete doc) using findOne query with _id and tokens.owner.
  2. If updated_at of the document is greater than tokens.owner created_at then return false.
  3. If I want to renew the token of the any tokens.owner, then it should update not append to tokens object.

I am learning api building in go lang with mongodb, Please help me.

Thanks.

0

There are 0 best solutions below