How to decrypt mongodb objectId on Nodejs CosomosDB Trigger

笑着哭i 提交于 2020-06-28 03:57:08

问题


I am retrieving my azure cosmosdb/mongodb document from a custom trigger to azure functions.. But my objectId seems to be encrypted.. How to get the correct objectid..

for example ObjectId("5df88e60d588f00c32a3c9ce") is coming as ]øŽ`Õˆð2£ÉÎ or ObjectId("5df88f92d588f00c32a3c9d1") is coming as ]ø’Õˆð2£ÉÑ

Is there a way to retrieve objectid in nodejs/python or any script if i give ]ø’Õˆð2£ÉÑ as input.

This is my function.json used in the azure function

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "documents",
      "direction": "in",
      "leaseCollectionName": "leases1",
      "connectionStringSetting": "devcosmosdb_DOCUMENTDB",
      "databaseName": "devcosmosdb",
      "collectionName": "newCollection",
      "createLeaseCollectionIfNotExists": "true"
    }
  ]
}

This is my nodejs code..

module.exports = async function (context, documents) {
    if (!!documents && documents.length > 0) {
        context.log('Document Id: ', documents[0].id);
        context.log(documents[0]);
    }
}

This is my output and this is where i am not getting the objectid properly..

2020-06-16T17:16:38Z   [Information]   Executing 'Functions.changeTrigger' (Reason='New changes on collection newCollection at 2020-06-16T17:16:38.2618864Z', Id=adc9556a-133f-4e85-b533-5574283a5a7d)
2020-06-16T17:16:38Z   [Information]   Document Id:  NWRmODhkZGRkNTg4ZjAwYzMyYTNjOWNj
2020-06-16T17:16:38Z   [Information]   {
  id: 'NWRmODhkZGRkNTg4ZjAwYzMyYTNjOWNj',
  _rid: 'KEcnAO163B4EAAAAAAAAAA==',
  _self: 'dbs/KEcnAA==/colls/KEcnAO163B4=/docs/KEcnAO163B4EAAAAAAAAAA==/',
  _ts: 1592327797,
  _etag: '"0000c1d2-0000-0300-0000-5ee8fe750000"',
  '$t': 3,
   '$v': {
    _id: { '$t': 7, '$v': ']øÝÕð\f2£ÉÌ' },
    name: { '$t': 2, '$v': 'myname' },
     email: { '$t': 2, '$v': 'my email' },
},
  _lsn: 537
}

回答1:


Please go to Azure portal to check the content of your document. I have done a test on my side, it just works fine.

Here is the document I used to test.

{
    "id": "testid1",
    "test1":"testvalue1",
    "test2":{
        "test21":"test21value",
        "objectId":"5df88f92d588f00c32a3c9d1"
    }
}

After clicking save button, the function will be triggered.

Here is my testing code.

def main(documents: func.DocumentList) -> str:
    if documents:
        logging.info('Document id: %s', documents[0]['id'])
        logging.info('%s',documents[0].to_json())

The output is as below.

Update:

Currently only SQL API base is supported in Azure function cosmosdb trigger. You can also find the feature under Settings part.

The URI should be something like https://testbowman.documents.azure.com:443/

If you create a mongodb api cosmosdb account, you won't find the 'add to function' feature. And the URI should be something like https://tonycosmosdb.mongo.cosmos.azure.com:443/



来源:https://stackoverflow.com/questions/62322016/how-to-decrypt-mongodb-objectid-on-nodejs-cosomosdb-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!