MongoDB missing basic document(object) operators?

Hello

How to do in mongoDB document operations?
Lets say that “$$mydoc” is a document
and “$$mykey” is a key (variable key not literal)

How to do

contains?("$$mydoc","$$mykey")
put("$$mydoc","$$mykey","randomValue")
remove("$$mydoc","$$mykey")
....
(and all those basic operation on maps that programming languages offer)

The root of the problem is that in mongo seems that you can’t use variables to refer to fields.

I know that i can use $function operator and javascript,but those above are so common
things and so useful when processing json data.

If key is literal like (“myfield”) i can construct a document {…} or use merge objects.
But if the key comed from variable the only way i know to do those is use object to array,and array to object,
but those cant be the normal way to update a document.If you need to do many updates it will be
so slow,and code so complicated.

I like mongodb alot,is there a way those to be added?
We can ask somewhere for new features we need to added in mongoDB query languange?

Thank you

1 Like

Hi @Takis,

Could you clarify where are you trying to perform these document operations ?
If you’re referring to MongoDB Aggregation Pipeline, perhaps you could utilise $let.

Also, if it’s an aggregation pipeline could you share the context on those operations (i.e. contains, put, remove) ?

  • Example document
  • Aggregation pipeline that you’ve tried
  • The expected output

Regards,
Wan.

1 Like

Hello @wan ,

Use cases are so many,everytime that you want to edit one embeded document
using a variable key (not literal, for example you didnt know it at pipeline writing time,you found it at pipeline run time) it is a use case.

Every programming language that has hash-maps provides those methods to process those
hash-maps.

Lets say you are in pipeline and you have this

  $$mydoc   = one document
  $$mykey   = one key
  
How to do 

  get("$$mydoc","$$mykey")
  contains?("$$mydoc","$$mykey")
  put("$$mydoc","$$mykey","randomValue")
  remove("$$mydoc","$$mykey")
  ....
  (and all those basic operation on maps that programming languages offer)

I managed to make so far only put,in a fast way using mergeObjects and objectToArray.
But it wasn’t straightforward at all.

For arrays its so easy to do a put it in mongoQL
{"$concatArrays"  ["$$array1" ["$$newmember"]])}
I want something like the above but with embeded documents

If you want to see an use example see this post,where i tried to group an array
using reduce and i couldnt do it.

ObjectToArray and ArrayToObject are ok if document is small or if you will do it one time.
If you are in a reduce,i cant convert the object to array and back to object at each step.

Its like saying to a Javascript programmer,dont add to an Object make the object array first,add to array,and then back to Object,its too slow to be usable.

Thank you

Hi @Takis,

Thanks for providing the background context.

You won’t be able to fully substitute a programming language with a database query language. A programming language will have more expressive operators, and a database can manipulate the data only to a certain extent (i.e. aggregation).

Instead of attempting to shift a complex computation from the application to the database, it would be better to either keep the operations at application side, and/or re-design the schema. See also Building With Patterns: A Summary.

In regards to the other thread, it looks like the thread ended with an answer. If that doesn’t work, you should clarify further what’s difference between your expected outcome and the response.

Regards,
Wan

2 Likes

There’s actually a JIRA ticket for this functionality: https://jira.mongodb.org/browse/SERVER-30417

Feel free to watch it (and upvote it).

3 Likes

Thank you i was searching for that on JIRA also it would be nice we to have those,i upvoted
and i am watching it,but i don’t think i can create JIRA to ask for those.
if we have get we can do contains,and put using get and mergeobjects
(new value(we can do it even now) or updated).
I think remove is also needed,i don’t know a way to do it.


  $$mydoc   = one document
  $$mykey   = one key
  
  get("$$mydoc","$$mykey")   //we need this to be added
  contains?("$$mydoc","$$mykey")  //if we have get we can do it,with check if null/missing
  put("$$mydoc","$$mykey","randomValue")  //if we have get we can do it,with mergeObjects
  remove("$$mydoc","$$mykey")   //we need this to be added