0

I get dynamic query params in my REST api which contains keys starting with $ sign. for example $format and $cost.

I need to persist them into MongoDB.

When i checked for a solution in web, I come across a solution which says to convert my $ or dot(.) into equivalent unicode and save to database.

MongoDB documentation says that we can neither insert a field name starting with dollar($) nor field containing dot(.) in it.

Is there any other way to persist it into database? Because its a dynamic key and I don't have control over it.

Is there any inbuilt classes in Mongo Library which would internally convert my field into Mongo understandable format ?

Thanks for the answers.

user3678624
  • 55
  • 1
  • 9

1 Answers1

0

Consider the following (not possible) document :

{
    $randomKey1 : 'data1',
    .randomKey2: {f1:'aa',f2:'bb'}
}

Instead of that, try to store to the following structure :

{
    fields:[
    {key:"$randomKey1", value: "data1"},
    {key:".randomKey2", value: {f1:'aa',f2:'bb'}},
    ]
}
matthPen
  • 4,253
  • 1
  • 16
  • 16