无法将Point保存到EmbeddedDocument MongoDB Dlask Mongoengine中

时间:2016-03-21 19:50:15

标签: python mongodb mongoengine

我正在使用: python:3.4.2 MongoDB:3.2 烧瓶:0.10.1 MongoEngine:0.10.6

我正在尝试使用以下代码将点保存到PointField中:

from mongoengine import StringField, PointField, \
                        EmbeddedDocument, Document, EmbeddedDocumentField


class Location(EmbeddedDocument):
    name = StringField()
    point = PointField()

    def __str__(self):
        self.name


class Person(Document):
    name = StringField()
    location = EmbeddedDocumentField(Location)

    def __str__(self):
        return self.name

在控制台上试试这个:

>>> location = Location()
>>> location.name = "Arroz"
>>> location.point = {"type": "Point", "coordinates": [12,22]}
>>> person = Person()
>>> person.name = "Juan"
>>> person.location = location
>>> person.save()

但是当我尝试save()时,启动一个except:

mongoengine.errors.OperationError: Could not save document (location object expected, location array not in correct format)

知道发生了什么事吗?

1 个答案:

答案 0 :(得分:0)

{"type": "Point", "coordinates": [12,22]}

是MongoEngine如何将数据存储在数据库中。它这样做,所以它知道如何在阅读文档时将其转换为对象。但是,当您与字段交互时,您只需要处理坐标。

location.point = [12, 22]