pymongo upsert无法使用$set操作执行插入

2024-04-26 17:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个空的集合,并且有数千个条目要处理(条目可能有多余的内容,我希望对其同时使用更新和插入)。 我编写的python代码(使用pymongo):

for mydoc in alldocs:
   key = {'myid': mydoc['myid']}
   data = process_doc(mydoc)    # returns simple dictionary
   db.mydocs.update(key, {"$set": data}, upsert = True)

以下代码无法执行任何插入操作。收藏仍然是空的。但是,当我删除$set并使用简单的数据时,它工作得很好。我不能在upsert中使用$set吗?我想要$set的原因是这样就不会影响BSON的现有字段。有人能指导一下吗。我真的不知道该怎么办。

可复制代码:

from pymongo import Connection
DB_CONTENT_BASE_KEY = 'contentbase'

def connect_to_db(dbname, hostname = 'localhost', portno = 27017, **kwargs):
    connection = Connection(hostname, portno)
    dbConnection = connection[dbname]
    return dbConnection

class MetawebCustomCollectionBuilder(object):
    # key ought to be a dictionary to filter results from contentbase.
    def __init__(self, inDbConfig, outDbConfig, key = {}, verbose = False):
        self.verbose = verbose
        self.inDbConfig = inDbConfig
        self.inDb = connect_to_db(**inDbConfig)
        self.outDbConfig = outDbConfig
        self.outDb = connect_to_db(**outDbConfig)
        self.inDbContentBase = self.inDb[self.inDbConfig[DB_CONTENT_BASE_KEY]]
        self.outDbContentBase = self.outDb[self.outDbConfig[DB_CONTENT_BASE_KEY]]
        self.key = key
        self.in_db_collection_constraints()
        self.out_db_collection_constraints()

    def in_db_collection_constraints(self):
        self.inDbContentBase.ensure_index('mid')
        if self.verbose: print("Assured index on mid for inDbContentBase...")

    def out_db_collection_constraints(self):
        self.outDbContentBase.ensure_index('mid')
        if self.verbose: print("Assured index on mid for outDbContentBase...")

    def process_in_record(self, inRecord):
        outRecord = inRecord # [YET TO] continue from here...
        return outRecord

    def transit_collection(self):
        for record in self.inDbContentBase.find(self.key):
            outRecord = self.process_in_record(record)
            key = {'mid':outRecord['mid']}
            data = outRecord
            print key
            self.outDbContentBase.update(key, {"$set": data}, True)
        if self.verbose: print 'Done with transiting collection from in DB to out DB'

    def cleanup_out_collection(self):
        pass

    def in_db_sandbox(self):
        # To have tests and analytics placed in here corresponding to inDb.
        pass

if __name__ == '__main__':
    inDbConfig = {'dbname':'metaweb', 'contentbase': 'content'}
    outDbConfig = {'dbname': 'similarkind', 'contentbase': 'content'}
    mccb = MetawebCustomCollectionBuilder(inDbConfig, outDbConfig, verbose = True)
    mccb.transit_collection()

必须有一个预先存在的数据库inDb。从这个集合中,我想创建一个新的修改集合。


Tags: tokeyinselfforverbosedbdef
1条回答
网友
1楼 · 发布于 2024-04-26 17:29:28

你的说法是错误的

>>> import pymongo
>>> c = pymongo.Connection()

>>> db = c.mydb
>>> db.mydocs.find().count()
0
>>> db.mydocs.update({'myid': '438'}, {"$set": {'keyA':'valueA'}}, upsert = True)
>>> db.mydocs.find().count()
1
>>> db.mydocs.find_one()
{u'myid': u'438', u'keyA': u'valueA', u'_id': ObjectId('504c2fd1a694cc9624bbd6a2')}

相关问题 更多 >