Closed GitHunter0 closed 2 years ago
The error that you get simply means that you are passing an invalid json string in find()
. The query argument needs to be a single json object (you're passing 3 objects).
Maybe try something like this?
con$find('{
"user": "user2",
"settings":{"$elemMatch": {"label": "settings_B"}}
}')
Or otherwise try passing separate json strings to the different find()
parameters, see the documentation for ?mongo.
Ah I think I get it now, the second part of your query is actually a projection? Try passing them as separate json strings:
con$find('{"user": "user2"}', '{"name":1, "settings":{"$elemMatch": {"label": "settings_B"}}}')
Or alternatively to get one value:
iter <- con$iterate('{"user": "user2"}', '{"name":1, "settings":{"$elemMatch": {"label": "settings_B"}}}')
val <- iter$one()
print(val)
Thank you very much @jeroen , now the query is working with the specification below you provided:
con$find(query = '{"user": "user2"}', fields = '{"name":1, "settings":{"$elemMatch": {"label": "settings_B"}}}')
However, if I want to just return settings.inputs
, how can I do that? The conventional way of adding "settings.inputs": true
to fields
will not work in this case.
I'm not sure. You might have to use con$aggregate()
to manually define an aggregation pipeline with multiple filter and projection steps.
Got it @jeroen , thank you for the feedback, mongolite
is an important package in R ecosystem.
Hi folks,
Please, conside this MWE:
The code below returns
Error: Invalid JSON object:
While in
You can check in this link: https://mongoplayground.net/p/N44pluEV85L
Mongo Playground
the same query returns this:Any idea why that syntax is not working in
mongolite
? I'm really needing to perform$elemMatch
queries viamongolite
but I cannot find a syntax that works.Thank you