H2CO3 / avocado

Strongly-typed MongoDB driver for Rust
MIT License
81 stars 2 forks source link

No ability to specify ordered for insert many #4

Closed max-frai closed 5 years ago

max-frai commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

max-frai commented 5 years ago

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

H2CO3 commented 5 years ago

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 commented 5 years ago

Got it. Thank you for description ;)

H2CO3 commented 5 years ago

No problem!

max-frai commented 5 years ago

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 commented 5 years ago

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).