我目前正在将数据提取到MongoDB中,稍后需要将此数据提取到单独的应用程序中。此应用程序要求_id字段为32位整数。
确保将结果文档中的_id属性显式设置为唯一的32位整数。 source
我正在利用pymongo将文档插入到集合中。
def parse_tweet(in_t):
t = {}
t["text"] = in_t["text"]
t["shape"] = in_t["coordinates"]["coordinates"][0], in_t["coordinates"]["coordinates"][1]
return t
这给了我预期的文件:
{
"_id" : ObjectId("50a0de04f26afb14f4bba03d"),
"text" : "hello world",
"shape" : [144.9557834, -37.8208589],
}
如何将_id值显式设置为32位整数? 我不打算存储超过600万份文件。
答案 0 :(得分:2)
只需生成一个id并传递它。 Id可以是任何内容(数组除外)。
def parse_tweet(in_t):
t = {}
t["_id"] = get_me_an_int32_id
t["text"] = in_t["text"]
t["shape"] = in_t["coordinates"]["coordinates"][0], in_t["coordinates"]["coordinates"][1]
return t
你必须自己照顾它的独特性。 MongoDB只会确保您不存储重复值。但是,如果你获得了独特的价值观 - 这就是你的问题。