mongodb / mongo-rust-driver

The official MongoDB Rust Driver
https://www.mongodb.com/docs/drivers/rust/current/
Apache License 2.0
1.44k stars 164 forks source link

Program gets stuck at insert_one #652

Closed xerenahmed closed 2 years ago

xerenahmed commented 2 years ago

Versions/Environment

  1. What version of Rust are you using? rustc 1.60.0-nightly (f624427f8 2022-02-06)
  2. What operating system are you using? Darwin Eren-MacBook-Air.local 21.4.0 Darwin Kernel Version 21.4.0
  3. What versions of the driver and its dependencies are you using? (Run cargo pkgid mongodb & cargo pkgid bson) https://github.com/rust-lang/crates.io-index#mongodb:2.1.0 https://github.com/rust-lang/crates.io-index#bson:2.1.0
  4. What version of MongoDB are you using? (Check with the MongoDB shell using db.version()) 5.0.6
  5. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)? standole

Describe the bug

When I try insert a document, it gets stuck. If I try insert empty struct it's fine.

BE SPECIFIC:

[derive(Serialize, Deserialize, Debug, Clone)]

struct Points { trait_count_point: HashMap<i32, i32>, trait_by_value_point: HashMap<String, HashMap<String, i32>>, trait_by_value_not_exists_point: HashMap<String, i32>, }

[derive(Serialize, Deserialize, Debug, Clone)]

struct RarityData { points: Points, counts: HashMap<String, HashMap<String, i32>>, not_exists_counts: HashMap<String, i32>, have_trait_counts: HashMap<i32, i32>, }

[derive(Serialize, Deserialize, Debug, Clone)]

struct DBRarityData { data: RarityData, contract_address: String, last_update: DateTime, }

// insert let mut file = std::fs::File::open("debug/rarity_data.json")?; let mut data: DBRarityData = serde_json::from_reader(&mut file).map_err(|e| anyhow!("{}", e))?; app.mongo .database("Assets") .collection("Test") .insert_one(data, None) .await?; // stucking here

// json file { "data": { "points": { "trait_count_point": { "6": 100, "2": 31425, "1": 39995 }, "trait_by_value_point": { "Background": { "Purp": 927, "Blue": 891, "Deep Alpha": 9222, "Beige": 927, "OG": 930, "Green": 872, "Sherry": 946, "Yellow": 939, "Red": 881 }, "Weapon": { "Pixie Scepter": 2945, "Chainsaw": 2973, "Sai": 2599, "Crossbow": 2672, "Jet Pack": 18535, "Scythe": 2687, "Badass": 2710, "Red Sword": 2806, "Vitalik's Scepter": 27753, "Dual Swords": 2741, "Skateboard": 2891, "Mace": 2650, "Water Jetpack": 23386, "Stabbed": 2718, "Sharp af": 2900, "Dagger": 2831, "Guitar": 2564, "KGB": 26915, "Dynamite": 2672, "Flail": 2635, "Ember Sword": 2687, "Wings": 27753, "Pickaxe": 2814, "Hockey Stick": 2498, "Halberd": 2517, "Slingshot": 2856, "Ninja Turtles": 12924, "Bow and Arrow": 2441, "Pitchfork": 2357, "Baseball Bat": 2982, "Stop-loss": 2357 }, "Gender": { "Male": 211, "Female": 1101 }, "Type": { "Honorary": 35396 }, "Clothing": { "Tribal": 5921, "Flamin Shirt": 7862, "Arrow Eater": 19764, "Sir": 35496, "Yakuza": 42238, "Red Shirt": 6202, "Blue Moroi": 49261, "Nuts and Bols": 8000, "Punk": 5495, "Puffa": 7236, "Shielded Vest": 7179, "Orange Yoroi": 6703, "Recycled": 6420, "Admiral": 46673, "Black Yoroi": 6331, "Tiger Ninja": 6606, "Skeleton": 27753, "Shir-t-Neck": 6202, "Gray Yoroi": 7599, "Zombie": 5921, "Tuxedo": 35496, "Sock Ninja": 5846, "I see you": 6512, "I'm fine": 42238, "Mummy": 35496, "Coveralls X": 7353, "Ninjas Creed": 35496, "Corporal": 7474, "Green Kimono": 5665, "Sailor": 5736, "Nunchucks": 5528, "Red Warrior": 6420, "Scarecrow": 6245, "Navy Blue Yoroi": 7236, "Scarved": 6512, "Sleeveless Yello Vest": 6803, "Tanktop": 6420, "Commander": 44345, "Rainbow Warrior": 21682, "Flower Shirt": 7794, "Emerald Warrior": 6375, "Bandolier": 5430, "Sleeveless Purp Vest": 7599, "Suspenders": 6654, "Dagger": 34134, "Krang": 6202, "Sleeveless Yello Hoodie": 7236, "Oni": 25382, "Poncho Shirt": 5883, "Scout": 6331, "Dragonborn": 885000, "Happy Christmas": 6959, "Navy Yoroi": 6959, "Purp Robe": 8448, "Ninja Squad Armor": 38573, "Ninja-Chain Hoodie": 52152, "Olive Yoroi": 6202, "Judge": 6854, "Coveralls": 6038, "9-5": 6512, "Doc": 6245, "Indigo Kimono": 5883, "Ethshirt": 5630, "Apprentice": 6854, "Wings": 24680, "Light Blue Yoroi": 6906, "Bear Ninja": 6288, "Footmen": 5772, "Nuts": 7067, "Lieutenant": 34134, "Sleeveless Blu Hoodie": 7599, "Werewulf": 55406, "Ninja Hoodie": 6906, "Fearsome Armor": 6420, "Scarfed": 27753, "Dragon Shirt": 6606, "Cape and Nipples": 6288, "Choo Choo": 6654, "Blue Yoroi": 6906, "5-9": 7930, "Purp Armor": 6512, "Pumpkin": 20679 }, "Face": { "Coolio": 4177, "Ouch": 3881, "Robo Ninja": 23386, "Goblin Ninja": 21682, "Hehehe": 4215, "Teasing": 7294, "Laser Eyes": 88590, "Wink": 4524, "Whaat": 4661, "Bitcoin Eyes": 4086, "OneEye": 4546, "What": 4104, "PurpleShades": 4235, "Fox Mask": 24680, "Smiles": 4140, "Meh": 4104, "Surprised": 6906, "Moustache": 4293, "Moustache Hmm": 3787, "Eyes-Closed": 7123, "Mask": 25382, "Fireman": 22789, "Sleepy Dude": 4122, "Piercings": 4140, "Hey": 6959, "Mechanical Mask": 24680, "AlienMan": 126514, "BubbleGum": 4122, "Stoner Eyes": 3914, "LOL": 4832, "WAGMI": 21169, "Foolish": 7728, "Red Mask": 36970, "TriEyes": 3914, "Mr. Eyebrows": 3981, "Panda Ninja": 23386, "Teary Eyes": 4122, "Blue Mask": 26915, "BoneMan": 4395, "Scarface": 126514, "Angry Bird": 4122, "Vampire": 88590, "Ape": 88590, "Attentive": 7013, "Punk": 88590, "Dolla Signs": 4354, "MaskMan": 4140, "Oh Noo": 4416, "Mr. Clean": 3756, "Green Shades": 4637, "Hmm": 4502, "Beauty": 7013, "Mwaha": 4333, "Tiger Ninja": 21682, "Lady Ninja": 221325, "Beast": 25382, "High Ninja": 32874, "Spectator": 8949, "Hmmm": 5215, "LoveBug": 4050, "Mummy": 147583, "Googly Eyes": 4480, "RIP": 4104 }, "Hat": { "SucukluYumurta": 55406, "Turkey Headband": 9932, "Water Fountain": 7236, "Horns": 7930, "Tombstone": 7599, "Red Headband": 7930, "Santa": 6959, "Cock a Doodle Doo": 8527, "FlyTrap": 7413, "Spartan": 7123, "Blue and White Headband": 7663, "Meow": 18535, "Cup o' Noodles": 7353, "Ninja Headband": 22222, "Samurai Warrior": 7067, "Yellow Headband": 6331, "Feather Hat": 6959, "Full Bandana": 6753, "Baby": 32874, "Broken Helmet": 6703, "Bird Nest": 7474, "Starfish": 6079, "MiniRex": 7179, "Sucuklu Yumurta": 7663, "Make a Wish": 7862, "Lettuce": 8000, "Dagger Head": 38573, "White Hat": 6466, "Drinks Hat": 9513, "Defi Farmer": 8527, "Oni Horns": 7353, "Skull": 8949, "Angel Halo": 26915, "Bulky Head": 26915, "Rice Hat": 7663, "Yellow Hat": 46673, "Slides": 9038, "Winter Hat": 6959, "Purple Headband": 6245, "Crown": 8370, "Watermelon": 88590, "Reverse Baseball Cap": 5809, "Thug Life": 7663, "Pyramid Headband": 6854, "Sailor Hat": 8072, "Deer": 7474, "Laurel Cap": 6512, "Medusa": 52152, "Fireman Hat": 8293, "Baby Kongz": 88590, "Octopus": 6119, "Commander's Cap": 6854, "Turtle": 6703, "Viking Hat": 6559, "Eggs on Head": 6559, "Fire Helmet": 7474, "Tribal Feathers": 6559, "Spaghetti": 46673, "Slot Machine": 7474, "Spikes": 6654, "Aviator Goggles": 7353, "Foxy": 7294, "Mushrooms": 7179, "Magnifying Glasses": 80545, "Candle": 7599, "Glass Bottle": 8448, "Cactus": 5772, "Unicorn": 6906, "Head Mirror": 6331, "Brainz": 8293, "Safari Hat": 5495, "Toadz": 88590, "Pirate Hat": 7123, "Fez": 7794, "Blue and White Band": 5999, "Chick": 24680, "Nice Hat": 27753, "Sliced Pineapple": 6420, "Cowboy Hat": 6854, "Chicken": 6753, "Orange Cone": 6606, "Baseball Cap": 8000, "Daisy Head": 6512, "Beanie": 32874 }, "Ninja": { "Kortan": 916503, "Myoo": 916503, "CryptoKemal": 916503, "X": 916503, "BeGe": 916503, "Refik Anadol": 916503, "wtcN": 916503, "Dundar": 916503, "Peker": 916503, "Siesta": 916503, "Squanch": 916503, "Kendine": 916503, "islor": 916503, "Chris": 916503, "Melih": 916503, "ErhanÜnal": 916503, "BCC": 916503, "Reynmen": 916503, "Arda": 916503, "Umut": 916503, "Topkek": 916503, "Locksmith": 916503, "Okan": 916503, "Eddy": 916503, "CMYLMZ": 916503, "Selçuk Erdem": 916503, "Paradotor": 916503, "Freeman": 916503 } }, "trait_by_value_not_exists_point": { "Type": 100, "Hat": 884900, "Ninja": 100, "Background": 884900, "Gender": 884900, "Weapon": 884900, "Face": 884900, "Clothing": 884900 } }, "counts": { "Type": { "Honorary": 50 }, "Background": { "Beige": 1070, "Blue": 1118, "Green": 1146, "Yellow": 1054, "OG": 1066, "Red": 1133, "Sherry": 1045, "Purp": 1070, "Deep Alpha": 97 }, "Clothing": { "Ethshirt": 160, "Choo Choo": 135, "Flamin Shirt": 114, "Admiral": 19, "Blue Moroi": 18, "Black Yoroi": 142, "Rainbow Warrior": 41, "Scarfed": 32, "Zombie": 152, "Tiger Ninja": 136, "Coveralls": 149, "Sleeveless Yello Vest": 132, "Ninjas Creed": 25, "Judge": 131, "Bandolier": 166, "Gray Yoroi": 118, "Ninja-Chain Hoodie": 17, "Happy Christmas": 129, "Tanktop": 140, "Corporal": 120, "Sock Ninja": 154, "Wings": 36, "Orange Yoroi": 134, "Emerald Warrior": 141, "Tuxedo": 25, "Sleeveless Yello Hoodie": 124, "Olive Yoroi": 145, "Tribal": 152, "Flower Shirt": 115, "Sleeveless Purp Vest": 118, "Sleeveless Blu Hoodie": 118, "Blue Yoroi": 130, "Green Kimono": 159, "Oni": 35, "Nunchucks": 163, "Ninja Squad Armor": 23, "Ninja Hoodie": 130, "Pumpkin": 43, "Yakuza": 21, "Red Shirt": 145, "Scarecrow": 144, "Arrow Eater": 45, "Recycled": 140, "Poncho Shirt": 153, "Skeleton": 32, "Suspenders": 135, "Navy Blue Yoroi": 124, "Lieutenant": 26, "Scarved": 138, "I see you": 138, "Dragon Shirt": 136, "Shir-t-Neck": 145, "5-9": 113, "Puffa": 124, "Fearsome Armor": 140, "Purp Armor": 138, "Sir": 25, "I'm fine": 21, "Dragonborn": 1, "Doc": 144, "Nuts": 127, "Mummy": 25, "Commander": 20, "Werewulf": 16, "Red Warrior": 140, "Punk": 164, "Indigo Kimono": 153, "Scout": 142, "Dagger": 26, "Krang": 145, "Nuts and Bols": 112, "9-5": 138, "Light Blue Yoroi": 130, "Bear Ninja": 143, "Sailor": 157, "Coveralls X": 122, "Purp Robe": 106, "Navy Yoroi": 129, "Footmen": 156, "Shielded Vest": 125, "Apprentice": 131, "Cape and Nipples": 143 }, "Weapon": { "Badass": 339, "Ninja Turtles": 69, "Mace": 347, "Hockey Stick": 369, "Dynamite": 344, "Sharp af": 316, "Red Sword": 327, "Halberd": 366, "KGB": 33, "Water Jetpack": 38, "Pitchfork": 392, "Dual Swords": 335, "Vitalik's Scepter": 32, "Skateboard": 317, "Ember Sword": 342, "Bow and Arrow": 378, "Slingshot": 321, "Wings": 32, "Dagger": 324, "Chainsaw": 308, "Sai": 354, "Pixie Scepter": 311, "Stop-loss": 392, "Guitar": 359, "Jet Pack": 48, "Baseball Bat": 307, "Scythe": 342, "Crossbow": 344, "Flail": 349, "Pickaxe": 326, "Stabbed": 338 }, "Gender": { "Female": 884, "Male": 7915 }, "Ninja": { "Okan": 1, "Peker": 1, "BCC": 1, "X": 1, "Umut": 1, "Dundar": 1, "Myoo": 1, "Kendine": 1, "BeGe": 1, "ErhanÜnal": 1, "Siesta": 1, "Freeman": 1, "Arda": 1, "Eddy": 1, "Selçuk Erdem": 1, "Kortan": 1, "CryptoKemal": 1, "CMYLMZ": 1, "Topkek": 1, "Refik Anadol": 1, "islor": 1, "Squanch": 1, "wtcN": 1, "Chris": 1, "Locksmith": 1, "Melih": 1, "Reynmen": 1, "Paradotor": 1 }, "Face": { "Mechanical Mask": 36, "Mr. Clean": 242, "Meh": 221, "PurpleShades": 214, "Red Mask": 24, "Teasing": 123, "Ouch": 234, "Hmmm": 173, "Moustache Hmm": 240, "Robo Ninja": 38, "Ape": 10, "Dolla Signs": 208, "AlienMan": 7, "Hehehe": 215, "Beauty": 128, "Fox Mask": 36, "Hey": 129, "Piercings": 219, "Smiles": 219, "Surprised": 130, "Coolio": 217, "Panda Ninja": 38, "Tiger Ninja": 41, "Goblin Ninja": 41, "What": 221, "Wink": 200, "Bitcoin Eyes": 222, "Scarface": 7, "Laser Eyes": 10, "OneEye": 199, "Whaat": 194, "Hmm": 201, "WAGMI": 42, "Blue Mask": 33, "Angry Bird": 220, "Googly Eyes": 202, "Attentive": 128, "Mummy": 6, "Foolish": 116, "Eyes-Closed": 126, "Mask": 35, "Green Shades": 195, "BoneMan": 206, "Fireman": 39, "Teary Eyes": 220, "BubbleGum": 220, "RIP": 221, "Mr. Eyebrows": 228, "Oh Noo": 205, "Sleepy Dude": 220, "Lady Ninja": 4, "Mwaha": 209, "Punk": 10, "LOL": 187, "LoveBug": 224, "Stoner Eyes": 232, "TriEyes": 232, "Beast": 35, "Vampire": 10, "Spectator": 100, "MaskMan": 219, "High Ninja": 27, "Moustache": 211 }, "Hat": { "Purple Headband": 144, "Defi Farmer": 105, "White Hat": 139, "Slot Machine": 120, "Skull": 100, "Angel Halo": 33, "Deer": 120, "Head Mirror": 142, "Medusa": 17, "Eggs on Head": 137, "SucukluYumurta": 16, "Cactus": 156, "Fireman Hat": 108, "Daisy Head": 138, "Crown": 107, "Lettuce": 112, "Spartan": 126, "Bulky Head": 33, "Safari Hat": 164, "Rice Hat": 117, "Santa": 129, "Pyramid Headband": 131, "Blue and White Headband": 117, "Glass Bottle": 106, "Tribal Feathers": 137, "Starfish": 148, "Sailor Hat": 111, "Turkey Headband": 90, "Yellow Hat": 19, "Beanie": 27, "Samurai Warrior": 127, "Aviator Goggles": 122, "Spikes": 135, "Cock a Doodle Doo": 105, "Drinks Hat": 94, "Chick": 36, "Commander's Cap": 131, "Foxy": 123, "FlyTrap": 121, "Reverse Baseball Cap": 155, "Dagger Head": 23, "Laurel Cap": 138, "Oni Horns": 122, "Viking Hat": 137, "Sucuklu Yumurta": 117, "Turtle": 134, "Make a Wish": 114, "Nice Hat": 32, "Toadz": 10, "Tombstone": 118, "Spaghetti": 19, "Pirate Hat": 126, "Meow": 48, "Ninja Headband": 40, "Fez": 115, "Mushrooms": 125, "Blue and White Band": 150, "Sliced Pineapple": 140, "Feather Hat": 129, "Thug Life": 117, "Cup o' Noodles": 122, "MiniRex": 125, "Watermelon": 10, "Brainz": 108, "Slides": 99, "Candle": 118, "Octopus": 147, "Yellow Headband": 142, "Winter Hat": 129, "Baby Kongz": 10, "Magnifying Glasses": 11, "Cowboy Hat": 131, "Chicken": 133, "Orange Cone": 136, "Baseball Cap": 112, "Full Bandana": 133, "Baby": 27, "Fire Helmet": 120, "Red Headband": 113, "Broken Helmet": 134, "Bird Nest": 120, "Unicorn": 130, "Water Fountain": 124, "Horns": 113 } }, "not_exists_counts": { "Face": 1, "Clothing": 1, "Weapon": 1, "Hat": 1, "Ninja": 8821, "Background": 1, "Gender": 1, "Type": 8799 }, "have_trait_counts": { "2": 28, "1": 22, "6": 8799 } }, "contract_address": "0x8C186802b1992f7650Ac865d4CA94D55fF3C0d17", "last_update": { "$date": { "$numberLong": "1651590671886" } } }



**To Reproduce**
Steps to reproduce the behavior:
1. Prepare data. (from json etc)
2. Insert to db
3. Gets Stuck, no error in console.
xerenahmed commented 2 years ago

I tried bson::to_bson and it said the problem. Problem is usage of integer in hashmap key but I think that's a bug.

kmahar commented 2 years ago

Hi @xerenahmed, thank you for reaching out.

The reason for the error here is BSON format only allows the keys in documents to be strings, similar to how JSON objects must have string keys.

While serde_json will automatically convert i32 hashmap keys to strings during serialization, we've opted not to do this automatically for the time being since it can lead to user data being stored in an unexpected format in the database, and we'd rather users explicitly opt-in to whatever type they would like non-string keys to be stored as.

Couple options to work around this - 1) You could explicitly use Strings rather than i32s for the hashmap keys 2) You could use something like serde_with::serde_as to tell serde to serialize the hashmap in a different format which is supported, for example an array of key-value pairs as shown here.

github-actions[bot] commented 2 years ago

There has not been any recent activity on this ticket, so we are marking it as stale. If we do not hear anything further from you, this issue will be automatically closed in one week.

github-actions[bot] commented 2 years ago

There has not been any recent activity on this ticket, so we are closing it. Thanks for reaching out and please feel free to file a new issue if you have further questions.