-
Notifications
You must be signed in to change notification settings - Fork 127
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
Add function for running mongodb query directly #45
Comments
I wonder how this should look like. Given the variety of different options we have from the driver, wouldn’t it be better to expose a way to call the driver directly but with the repo's pool? |
That might be better, but I don't know what the would look like. We might be dropping the drivers own pooling in the future. |
We already inject a What do you think? |
That sounds like the best way of doing it. We can revisit again when (if) On Thu, Oct 22, 2015 at 9:44 AM, Michał Muskała [email protected]
Eric Meadows-Jönsson |
What's the status on this....is this possible yet? Unless I've missed how various MongoDB aggregations can be done, there appears to be quite a bit of variance between what can be accomplished in queries directly against MongoDB vs. through mongo_ecto. It seems that one way to leap past the immediate need to integrate individual aggregation operations into mongo_ecto would be just to be able to send a raw statement to MongoDB. Can this be done? |
This is how I do it and I think it's right -- it uses the same pool as used by ecto if I'm understanding the code correctly:
|
@scottmessinger Hey thanks for the reply. I have read through the mongo_ecto doc, and I confess, I haven't been able to figure out how to do this. Since you have a handle on it can you give me an idea of how to accomplish this MongoDB query through the mongo_ecto driver? db.snapshots.aggregate( I think maybe if I could see this example I could extrapolate from that and figure out related queries. |
@ericmj i am also looking for run mongo query directly using mongo_ecto driver, i tried a lot to execute join queries using mongo_ecto and not able to figure it out , basically we need it for run join queries using $lookup key, please let me know if you guys have any solution for it. Thanks in advance. |
You need to pass in the mongo pool from your ecto repository. Like this: |
@ericmj is Mongo.count(MyApp.Repo.Pool, "mycollection", nil) works. |
@iStefo I don't know, it's possible it works differently in the Ecto 2.0 branch of mongodb_ecto. |
@iStefo You will most likely want to use The reason you may want to do this instead of just using |
@ankhers thanks for the explanation! Would it make sense to include this bit in the documentation? I think it would be particularly useful for executing aggregation functions which are (understandably) not implemented in this Ecto adapter. |
I don't think so. That function is meant to be for internal use only (hence the leading and trailing double underscore). I do, however, think it would be beneficial to have something more similar to Unfortunately because of the differences between SQL and Mongo, I do not think it would be possible to provide a single |
It was only proposed as a workaround. We definitely want a function for this, that's why the issue is still open and up for grabs :) |
I'm not sure whether the original intent of this issue reflects the current state in the discussion: As @ankhers noticed, mongoDB can't be used with only one Instead of wrapping all these functions, I think it would be preferable to expose the connection pool in a documented manner (and call it a day from the view of this adapter). |
That is also a good way to move forward. Would you be able to send in a PR? |
Yep, I will try to do it tomorrow. |
2 questions: What name would you think is suitable for exposing the pool? Simple rename of Is there any other use case that requires accessing the pool? Otherwise it would be possible to proxy calls to the mongo driver by using def mongo_call(method, args \\ []) when is_atom(method) and is_list(args) do
{pool, _} = __pool__()
apply(Mongo, method, [pool | args])
end I think this approach is more elegant since it requires less boilerplate for the application developers. In the above snippet, the user would call Direct access to the pool would still be (semi)private to the Repo module, however. I honestly don't know whether this is desirable or not. |
With the mongo driver migrating to db_connection it's not as easy as just passing the right pool name. Some pool options need to also be passed so that db_connection can work properly. We probably need a wrapper around the entire interface of We could call it |
I see the changes made to the MongoDB module in https://github.com/ericmj/mongodb/compare/emj-dbconn#diff-6592a4605ea4a81c3fe573f99e1a1cb1. Has there already been some work done in this project to reflect the upcoming changes? What do you specifically mean with "doing the right thing"? Taking care of the underlying |
The changes are incorporated as part of the PR #84. Passing the pool name as the first argument is completely fine, but we should also be passing the pool implementation (db_connection supports multiple pools) in options and merge default pool, connection, and query timeouts. |
The documentation in https://github.com/ericmj/mongodb/blob/0687ef615e3a9a57156118e47dcd615e92b0e8a2/lib/mongo.ex does not yet reflect the changes that are already in the code so I'm not sure how passing generic(?) options to the Do I understand you correctly in that it would be possible for the user to do mongo_call(:aggregate, ["channels", [%{ "$group" => ... }], timeout: 2000]) and we would need to merge the Mongo.aggregate("channel", [%{ "$group" => ... }], [timeout: 2000, url: ..., adapter: ...]) As long as the convention of options-last holds, this seems like a feasible approach. To make it easier for us (remove the need of detecting and extracting a Keyword list from the list of arguments), we could change the method signature to |
You are looking at a commit from 2016, check out master instead. |
@ericmj thanks, is nicely documented in https://github.com/ericmj/mongodb/blob/master/lib/mongo.ex#L6 |
Would love to help, let me know if there is anything I can do. |
Basically the equivalent of
Ecto.Adapters.SQL.query
. We need to discuss what the API should be like because the driver exposes multiple functions where the SQL drivers only have one.I would be willing to work on this.
The text was updated successfully, but these errors were encountered: