【问题标题】:How to create index for dic in mongodb with python?如何使用python在mongodb中为dic创建索引?
【发布时间】:2017-02-23 18:58:18
【问题描述】:

我有一些对象,例如:

{
    'id':1
    'claims':{
        'c1':{xxxxx},
        'c8':{xxxxx},
        'c20':{xxxxx}
    }
}

如果我直接使用d.create_index([('claims', 1)]),它将使用整个{'c1':{xxxxx},'c8':{xxxxx},'c20':{xxxxx}} 作为索引,这不是我想要的。我只想使用'c1','c8','c20'作为键。我看了文档说如果是数组的话:

{
    'id':1
    'claims':[
        'c1':{xxxxx},
        'c8':{xxxxx},
        'c20':{xxxxx}
    ]
}

这将是可能的。我想知道当它是一个字典时是否可以做到这一点。或者如何将其转换为数组?

【问题讨论】:

    标签: python arrays mongodb dictionary indexing


    【解决方案1】:

    这样做的方法如下:

    db.collection.createIndex( { 'claims.c1':1, 'claims.c8':1, 'claims.c20':1 } )
    

    【讨论】:

      猜你喜欢
      • 2018-08-17
      • 1970-01-01
      • 2012-12-09
      • 2020-10-08
      • 2012-04-01
      • 1970-01-01
      • 2016-11-15
      • 2015-02-06
      • 2020-09-28
      相关资源
      最近更新 更多