Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

missed possibility to use aggregation pipeline in $update #260

Open
eutanatos opened this issue Dec 20, 2023 · 0 comments
Open

missed possibility to use aggregation pipeline in $update #260

eutanatos opened this issue Dec 20, 2023 · 0 comments

Comments

@eutanatos
Copy link

According Mongodb manual I can use in either document {} or pipeline [] and it can lead to different results:

document

db.Collection.insertMany( [
{"_id" : 1, "links" : [ {link_1: "url1", link_2: "url2"} ] },
{"_id" : 2, "links" : [ {link_1: "url3", link_2: "url4"} ] }
] )

db.Collection.updateMany( {}, { "$set": {"links: {"$arrayElemAt": ["$links", 0]}} } )

db.Collection.findOne( {} )
// {_id: 1, links: {'$arrayElemAt': ['$links', 0 ] } }

pipeline

db.Collection.insertMany( [
{"_id" : 1, "links" : [ {link_1: "url1", link_2: "url2"} ] },
{"_id" : 2, "links" : [ {link_1: "url3", link_2: "url4"} ] }
] )
db.Collection.updateMany( {}, [{ "$set": {"links: {"$arrayElemAt": ["$links", 0]}} }] )

db.Collection.findOne( {} )
// {_id: 1, links: { link_1: "url1", link_2: "url2" } }

Mongolite accepts only document form of update request with consequent loose of possibility of mongo functions usage:

mongo_collection$update('{}', update = '{"$set": {"links": { "$arrayElemAt": ["$links", 0] } } }', multiple = TRUE)
mongo_collection$find('{}')
# {_id: 1, links: {'$arrayElemAt': ['$links', 0 ] } }

mongo_collection$update('{}', update = '[{"$set": {"links": { "$arrayElemAt": ["$links", 0] } } }]', multiple = TRUE)
# Error: Invalid JSON object: [{"$set": {"links": {$arrayElemAt: ["$links", 0]} } }]

I know it can be overrided by $aggregation with merge in same collection, but can you add this functionality?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant