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

No ability to specify ordered for insert many #4

Closed
max-frai opened this issue Jun 28, 2019 · 9 comments
Closed

No ability to specify ordered for insert many #4

max-frai opened this issue Jun 28, 2019 · 9 comments

Comments

@max-frai
Copy link

Hello, there is no way to specify for insert_many ordered option: https://docs.rs/mongodb/0.3.12/mongodb/coll/options/struct.InsertManyOptions.html#structfield.ordered

It's useful, when we insert vector of data and some fields could fail on insert (for example, because of unique index). When we set ordered to false, mongodb will ignore errors and try to insert as much as possible.

@H2CO3
Copy link
Owner

H2CO3 commented Jun 28, 2019

Hey there!

Sorry, what exactly do you mean by "there is no way to specify" this option? I think that's exactly what the Doc::insert_options() method does.

@max-frai
Copy link
Author

Sorry, I can't understand where to use it. There are no arguments for collection insert_many, so the only way is to declare custom query. Bu there is only Update or Upsert.

@H2CO3
Copy link
Owner

H2CO3 commented Jun 28, 2019

You implement/override this method on the type that implements the Doc trait.

@max-frai
Copy link
Author

So I can't apply it to only one query, and only for all Doc structure?

@H2CO3
Copy link
Owner

H2CO3 commented Jun 28, 2019

Yes. Doing otherwise would be surprising (in a bad way).

To be clear: you specify it per document. You can still have it behave differently for different document types. But the point is that a certain document type should behave uniformly in as many regards as possible, especially in cases like insertion, which is only about the document type and it doesn't intrinsically have anything to do with some other data structure (like queries or updates would have).

@max-frai
Copy link
Author

Got it. Thank you for description ;)

@H2CO3
Copy link
Owner

H2CO3 commented Jun 28, 2019

No problem!

@max-frai
Copy link
Author

Sorry again.

#[derive(Debug, Serialize, Deserialize, Clone, Doc)]
pub struct Test {
    _id: Uid<Test>,
}

impl Test {
    fn insert_options() -> InsertManyOptions {
        println!("!");
        InsertManyOptions {
            ordered: Some(false),
            write_concern: None,
        }
    }
}

This insert_options is not called. I also set debug print in your library inside insert_many and the options are default.

Is it possible to use proc macro to define doc with indexes and replace function?

@H2CO3
Copy link
Owner

H2CO3 commented Jun 29, 2019

impl Test {

This doesn't impl Doc for Test, it defines an identically-named inherent method (i.e. one that is not part of the Doc trait) for the concrete type Test, so it is not going to be called when <Test as Doc>::insert_options() is called.

Unfortunately, there's currently no way in the proc-macro derive for Doc to replace individual functions, but it should probably be possible. I'll look into that soon. I'd suggest you to open a separate issue for that, as it's really a different question and different feature request (which does make sense and I think should be implemented somehow).

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

2 participants