将项添加到mongoengine中的ReferenceField列表中

时间:2018-04-13 11:20:17

标签: python pymongo mongoengine

我在mongo中拥有以下我的收藏结构。

Date    Country         Followers
2018-01-01  Australia   7912
2018-01-01  Austria     4365
2018-01-01  Belgium     1343
2018-01-02  Australia   7931
2018-01-02  Austria     4364
2018-01-02  Belgium     1343
2018-01-03  Australia   7930
2018-01-03  Austria     4366
2018-01-03  Belgium     1344
2018-01-04  Australia   7928
2018-01-04  Austria     4365
2018-01-04  Belgium     1345
2018-01-05  Australia   7929
2018-01-05  Austria     4362
2018-01-05  Belgium     1347
2018-01-06  Australia   7939
2018-01-06  Austria     4363
2018-01-06  Belgium     1347
2018-01-07  Australia   7950
2018-01-07  Austria     4361
2018-01-07  Belgium     1348
2018-01-08  Australia   7933
2018-01-08  Austria     4339
2018-01-08  Belgium     1343

SkillCluster集合的示例文档

class SkillCluster(DynamicDocument):
    Name = StringField(required=True, unique=True)
    SubClusters = ListField(StringField())
    ModifiedDate = DateTimeField(default=datetime.utcnow)
    meta = {
        'indexes': ['ClusterName'],
        'index_background': True,
        'collection': 'SkillClusters',
        'db_alias': db_alias_name
    }


class Skill(DynamicDocument):
    Name = StringField(required=True, unique=True)
    Clusters = ListField(ReferenceField(SkillCluster))
    ModifiedDate = DateTimeField(default=datetime.utcnow)
    meta = {
        'indexes': ['SkillName'],
        'index_background': True,
        'collection': 'Skill',
        'db_alias': db_alias_name
    }

技能可能属于不同的群集,我的{ "_id" : ObjectId("5ad089263b3f4f5d68d74d7f"), "Name" : "Sciences", "ModifiedDate" : ISODate("2018-04-13T10:40:38.152Z"), "SubClusters" : [ "Biology", "Biotech", "Chemistry", "General" ] } 集合中有Clusters列表字段。我已经提出了以下代码来添加技能

Skill

但是我收到了这个错误 -

def add_skills(skill_name, cluster_string):
    cluster_sub_clusters = cluster_string.split(':', 1)
    cluster_name, sub_cluster_name = cluster_sub_clusters[0], cluster_sub_clusters[-1].strip()
    # query to test if the "SkillCluster" collection has a document that has both the cluster_name in "Name" fields & sub_cluster_name in "SubClusters" array 
    result_set = SkillCluster.objects(Name=cluster_name, SubClusters__contains=sub_cluster_name)
    if result_set:
        data_dict = {
            "ModifiedDate": datetime.utcnow(),
            "Clusters": {
                "$addToSet": result_set[0]
            }

        }
        skill = Skill.objects(Name=skill_name).modify(upsert=True, new=True, **data_dict)
    else:
        # check if document exists with name = cluster_name, if so then add sub_cluster_name to the "SubClusters" array
        result_set = SkillCluster.objects(Name=cluster_name)
        if result_set:
            result_set[0].update(add_to_set__SubClusters=sub_cluster_name)
            # ........... rest of the logic

我应该如何向ListField添加引用对象。

谢谢。

1 个答案:

答案 0 :(得分:0)

Modify函数的语法引起了问题。

MongoEngine对update()modify()函数都使用Django样式的update关键字参数。所以也许您应该尝试这样的事情:

data_dict = { 
    #"ModifiedDate": datetime.utcnow(),
    add_to_set__Clusters : result_set[0] # <= Django-style update/modify keyword arguments
} 
skill = Skill.objects(Name=skill_name).modify(upsert=True, new=True, **data_dict)

以下是指向MongoEngine API参考的链接:http://docs.mongoengine.org/apireference.html搜索update()modify(),还可以参考atomic-updates部分以获取可能的更新关键字列表:{{3} }