在嵌套文档中生成唯一id - Pymongo
我的数据库是这样的.
{
"_id":"5ea661d6213894a6082af6d1",
"blog_id":"blog_one",
"comments": [
{
"user_id":"1",
"comment":"comment for blog one this is good"
},
我需要帮助来理解这里发生了什么,并提供一个避免这种情况的建议!
下面是我的代码片段:
result = [list of dictionary objects(dictionary objects have 2 keys and 2 String values)]
copyResults = list(results);
## Here I try to insert each Dict into MongoDB (Using PyMongo)
for item in copyResults:
dbcollection.save(
在下面的代码中,我按日期查询并按名称排序(我没有索引日期字段听起来很奇怪,但我这样做是为了避免在内存中排序,这就是我按名称索引的原因)。如果我运行一个explain,我会得到以下结果:
-> index on name
cursor: BtreeCursor name_1
scanAndOrder: False
nscanned: 1000
nscannedObjects: 1000
n:49
millis:1
然后,如果我创建一个包含名称和日期的复合索引,我会得到以下输出:
-> index on name + date
cursor: BtreeCursor name_1_da