如何在MongoEngine的ListField中的EmbeddedDocument上进行原子更新? [英] How to Do An Atomic Update on an EmbeddedDocument in a ListField in MongoEngine?

查看:288
本文介绍了如何在MongoEngine的ListField中的EmbeddedDocument上进行原子更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow上发现了一些类似的问题,但是没有任何内容可以满足我的需求,因此我们将不胜感激.

I've found some similar questions on StackOverflow, but nothing that addresses what I'm looking for, so any help would be appreciated.

我的模特:

class BlogPost(EmbeddedDocument):
  title = StringField(required=True)
  blog_url = StringField(required=True, unique=True)
  content = StringField()
  turned_into_bitly_link = BooleanField(default=False)

class Person(Document):
  name = StringField
  blog_posts = ListField(EmbeddedDocumentField(BlogPost), default=list)

对于每个blogpost.blog_url,我查询Bitly API以查看URL是否已缩短.我需要做的就是将我从Bitly获得的数据与数据库中相应的博客帖子进行匹配.我从Bitly取回的对象包含一个url字段,我需要使用该字段来匹配并更新数据库中的相应博客文章.还应该说我一次将一批blog_urls发送给Bitly,一个选项也不行.

For each blogpost.blog_url, I query the Bitly API to see if the url has been shortened. What I need to be able to do is match up the data I get back from Bitly with the appropriate blogpost in my database. The objects I get back from Bitly contain a url field that I need to use to match up and update the appropriate blogpost in my database. Should also say that I send a batch of blog_urls to Bitly at a time, one-by one is not an option.

给出一组Blog_posts和Bitly对象,这些对象均来自给定的个人: person = Person.objects.get(name__exact ='BobSmith')

Given a set of blog_posts and Bitly objects all stemming from a given individual: person = Person.objects.get(name__exact='BobSmith')

如何通过唯一的URL字段选择嵌入到我的Person对象中的特定blog_post?

How can I select the specific blog_post embedded in my Person object by the unique URL field?

作为一个权宜之计,我想可以遍历我的person对象中的blog_posts,如果blog_post.url与我的Bitly对象中的URL匹配,则可以更新Turned_into_bitly_link字段,但是我不确定这是最有效的方法.

As a stopgap, I suppose I could iterate over the blog_posts in my person object, and if the blog_post.url matches the URL in my Bitly object, I can then update the turned_into_bitly_link field, but I'm not sure this is the most efficient way of going about this.

希望这是有道理的.我很高兴澄清一下,并在此先感谢您的任何建议.

Hope this makes sense. I'm happy to clarify, and thanks in advance for any advice.

推荐答案

您可以使用位置运算符来更新匹配的嵌入式文档.

You can use the positional operator to update the matched embedded document.

这里有一个测试示例(https://github.com/MongoEngine/mongoengine/blob/master/tests/test_queryset.py#L313)

Heres an example from the tests (https://github.com/MongoEngine/mongoengine/blob/master/tests/test_queryset.py#L313)

def test_update_using_positional_operator(self):
    """Ensure that the list fields can be updated using the positional
    operator."""

    class Comment(EmbeddedDocument):
        by = StringField()
        votes = IntField()

    class BlogPost(Document):
        title = StringField()
        comments = ListField(EmbeddedDocumentField(Comment))

    BlogPost.drop_collection()

    c1 = Comment(by="joe", votes=3)
    c2 = Comment(by="jane", votes=7)

    BlogPost(title="ABC", comments=[c1, c2]).save()

    BlogPost.objects(comments__by="jane").update(inc__comments__S__votes=1)

    post = BlogPost.objects.first()
    self.assertEquals(post.comments[1].by, 'jane')
    self.assertEquals(post.comments[1].votes, 8)

这篇关于如何在MongoEngine的ListField中的EmbeddedDocument上进行原子更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆