Repository: PoloDB/PoloDB (Rust)

Issue:
Null is used as _id when Option<ObjectId> is None.
I use a model like this:

```rust
/// Structure for storing client registration data
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ClientSettings {
    pub _id: Option<polodb_core::bson::oid::ObjectId>,
    /// Generic client name
    pub name: String,
    /// Hostname of the client
    pub hostname: String,
    /// UUID of the client
    pub client_id: String,
    /// Secret for authentication
    pub client_secret: String,
    /// Type of system
    pub typ: ClientType,
    /// Date of last modification
    pub last_modified: DateTime,
    /// Date of creation
    pub created: DateTime,
    /// Collection of facts about the client
    pub facts: HashMap<String, String>,
}
```

When the _id value is None and `collection.insert_one(&client)` is called, the returned
inserted_id is `Null`.
When the document is serialized to bson, the _id is set to Null.
This is then used as an id, instead of generating a real one.

If I remove the _id field, the insert operation returns a proper _id.


Discussion:

Patch:
diff --git a/src/polodb_core/db/db_inner.rs b/src/polodb_core/db/db_inner.rs
index 3160108d..ed15546c 100644
--- a/src/polodb_core/db/db_inner.rs
+++ b/src/polodb_core/db/db_inner.rs
@@ -456,8 +456,11 @@ impl DatabaseInner {
 
     #[inline]
     fn fix_doc(mut doc: Document) -> Document {
-        if doc.get(meta_doc_key::ID).is_some() {
-            return doc;
+        if let Some(id) = doc.get(meta_doc_key::ID) {
+            // If the id type is not null, the document is ok
+            if id.as_null().is_none() {
+                return doc;
+            }
         }
 
         let new_oid = ObjectId::new();


Test patch:
diff --git a/src/polodb_core/tests/test_insert.rs b/src/polodb_core/tests/test_insert.rs
index 992311a3..57e168b6 100644
--- a/src/polodb_core/tests/test_insert.rs
+++ b/src/polodb_core/tests/test_insert.rs
@@ -23,6 +23,13 @@ struct Book {
     author: String,
 }
 
+#[derive(Debug, Serialize, Deserialize)]
+struct IdBook {
+    _id: Option<bson::oid::ObjectId>,
+    title: String,
+    author: String,
+}
+
 #[test]
 fn test_insert_struct() {
     vec![
@@ -66,6 +73,62 @@ fn test_insert_struct() {
     });
 }
 
+
+#[test]
+fn test_insert_id_struct() {
+    vec![
+        prepare_db("test-insert-id-struct").unwrap(),
+        Database::open_memory().unwrap(),
+    ].iter().for_each(|db| {
+        // Get a handle to a collection of `Book`.
+        let typed_collection = db.collection::<IdBook>("books");
+
+        let books = vec![
+            IdBook {
+                _id: None,
+                title: "A treatise on electricity and magnetism Vol I".to_string(),
+                author: "James Clerk Maxwell".to_string(),
+            },
+            IdBook {
+                _id: None,
+                title: "Sidelights on Relativity".to_string(),
+                author: "Albert Einstein".to_string(),
+            },
+        ];
+
+        // Insert the books into "mydb.books" collection, no manual conversion to BSON necessary.
+        match typed_collection.insert_many(books) {
+            Ok(result) => {
+                assert_eq!(result.inserted_ids.len(), 2);
+                result.inserted_ids.get(&0usize).unwrap().as_object_id().expect("should be an object id");
+                result.inserted_ids.get(&1usize).unwrap().as_object_id().expect("should be an object id");
+            },
+            Err(e) => {
+                panic!("{}", e.to_string())
+            }
+        };
+
+        
+        let id_3 = match typed_collection.insert_one(    IdBook {
+            _id: None,
+            title: "Basic Structures of Matter: Supergravitation Unified Theory".to_string(),
+            author: "Stoyan Sarg".to_string(),
+        }) {
+            Ok(result) => {
+                result.inserted_id.as_object_id().expect("should be an object id")
+            },
+            Err(e) => {
+                panic!("{}", e.to_string())
+            }
+        };
+
+        let book = typed_collection.find_one(doc! {
+            "_id": id_3,
+        }).unwrap().unwrap();
+        assert_eq!(book.author, "Stoyan Sarg");
+    });
+}
+
 #[test]
 fn test_insert_bigger_key() {
     vec![