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

Sus*_*pta 1 python upsert mongodb pymongo

我有一个空集合,有数千个条目要处理(条目可能有减少,我想要使用更新和插入).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)
Run Code Online (Sandbox Code Playgroud)

以下代码无法执行任何插入操作.该系列仍然是空的.但是,当我删除$ 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()
Run Code Online (Sandbox Code Playgroud)

在db中必须有一个先前存在的数据库.从这个集合我想创建一个新的修改集合.

小智 8

你的主张是错的

>>> 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')}
Run Code Online (Sandbox Code Playgroud)