简体   繁体   English

如何将数据插入 MongoDB (Python, Pymongo)

[英]How to insert data into MongoDB (Python, Pymongo)

So i know that in pymongo we create dictionaries to represent documents.所以我知道在 pymongo 中我们创建字典来表示文档。

Here is the data I need to store into mongodb这是我需要存储到 mongodb 中的数据

"Dog Snails Zebra Horse Fox".split() "狗蜗牛斑马马狐狸".split()

Pet = [Dog, Snails, Fox, Horse, Zebra ]宠物 = [狗、蜗牛、狐狸、马、斑马]

^^ How would I go about converting either of these into a dictionary so I can store the data in mongoDB? ^^ 我将如何将其中任何一个转换为字典,以便将数据存储在 mongoDB 中?

As I know you have to have the format我知道你必须有格式

emp_rec1 = { "_id": 1 "name":"Mr.Geek", "eid":24, } emp_rec1 = { "_id": 1 "name":"Mr.Geek", "eid":24, }

But as I only have one key (pets) and many values (dogs, cats, snails, Fox, Horse, Zebra) how would I do this?但是由于我只有一把钥匙(宠物)和许多值(狗、猫、蜗牛、狐狸、马、斑马),我该怎么做?

In MongoDB, data is stored as documents.在 MongoDB 中,数据存储为文档。 These documents are stored in MongoDB in JSON (JavaScript Object Notation) format.这些文档以 JSON(JavaScript Object Notation)格式存储在 MongoDB 中。 So you can insert as { "pet":[ "Dog", "Snails", "Fox", "Horse", "Zebra" ] }所以你可以插入 { "pet":[ "Dog", "Snails", "Fox", "Horse", "Zebra" ] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM