Skip to main content
 首页 » 编程设计

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

2025年05月04日1352php

我有一个空集合并且有数千个条目要处理(条目可能有冗余,我想同时使用更新和插入)。 我写的 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() 

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

请您参考如下方法:

你的声明是错误的

>>> 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')}