【问题标题】:Creating indexes - MongoDB创建索引 - MongoDB
【发布时间】:2011-03-20 12:34:43
【问题描述】:

我的“桌子”是这样的:

{'name':'Rupert', 'type':'Unicorn', 'actions':[
    {'time':0, 'position':[0,0], 'action':'run'},
    {'time':50, 'position':[50,0], 'action':'stoprun'},
    {'time':50, 'position':[50,0], 'action':'jump'},
    {'time':55, 'position':[50,0], 'action':'laugh'},
    ...
]}

有什么方法可以索引操作列表中的项目?还是我必须将它们分成更多的表格?

将操作保留在当前表行中对我来说会方便得多。

【问题讨论】:

    标签: python django mongodb pymongo mongoengine


    【解决方案1】:

    pymongo 示例:

    import pymongo
    
    mongo = pymongo.Connection('localhost')
    collection = mongo['database']['hosts']
    collection.ensure_index('host_name', unique=True)
    

    【讨论】:

    • 如果要删除重复项:collection.ensure_index('host_name', unique=True, drop_dups=True)
    【解决方案2】:

    感谢#mongodb中的skot!!

    一种解决方案是:

    [...].ensureIndex({"actions.time":1})
    

    用于在操作列表中的时间字段上创建索引。

    【讨论】:

    • 给定的代码是javascript,实际的方法是python中的'ensure_index'
    猜你喜欢
    • 2020-10-08
    • 2012-12-09
    • 1970-01-01
    • 2014-12-02
    • 2015-11-08
    • 2011-08-20
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多