我有一个包含两个元素元组的列表,其中第一个元素是一个字符串(某个参数的名称),第二个元素是一个float(该参数的值).例如,
thelist = [('costperunit', 200), ('profit', 10000), ('fixedcost', 5000),
('numpeople':300)]
还有更多这样的元组,名称在实际案例中有所不同.我想将这些作为键:值对添加到mongoDB数据库.这是我想要添加它的方式.
db.collection.insert( {paramvalues: {'costperunit':200, 'profit':10000,
'fixedcost': 5000, 'numpeople': 300} } )
一种方法是:
dictform = dict(thelist)
db.collection.insert( {paramvalues: dictform} )
但是,这不会确保参数名称和值的顺序,因为dict会更改顺序.
我试过了
from collections import OrderedDict
dictform = OrderedDict(thelist)
db.collection.insert( {paramvalues: dictform} )
这将维护参数名称和值的原始顺序,但是,将参数名称和值作为列表列表插入.
我是mongoDB的新手,并试图学习它.在Python或mongoDB中是否有一个可以实现我想要的技巧?我想将Mongodb数据库中的键值参数值作为字典(或Javascript对象)的原因是我可以使用某个参数的值来过滤结果.例如,我可以这样做:
db.collection.find( {'paramvalues.costperunit': 200} )
如果你确定无法做到这一点,如果你让我知道,我将不胜感激.
谢谢.