开发者

Mongodb schema structure - push and pop

开发者 https://www.devze.com 2023-02-08 08:09 出处:网络
I\'d like to have something like the following schema for a collection: name1: - guid1 : [{ foo1}, {foo2}, .. {foo n}]

I'd like to have something like the following schema for a collection:

name1:
 - guid1 : [{ foo1}, {foo2}, .. {foo n}]
 - guid2 : [{ foo1}, {foo2}, .. {foo n}]

(1) Each of the foo objects are dictionaries with the same internal structure - and so ideally i would have a single dictionary开发者_C百科 below guid where i could append additional data - but this does not seem to be possible (or is it?)

(2) I'm struggling with being able to pop the data -i'm not sure how i can stucture a query to only pop a single guid off name1?

I'm using pymongo, if relevant.


Not to throw a wrench your way, but have you looked into using MongoEngine? It sits on top of pymongo, and makes working with mongoDB a joy. Check it out.

So your mongoengine declaration would look like this:

from mongoengine import *                           

class Foo(EmbeddedDocument):                  
    #Declare your Foo properties here

class Guid(EmbeddedDocument):                  
    name = StringField()                 
    foos = ListField(EmbeddedDocumentField(Foo))                                    

class Name(Document):                          
    title = StringField(required=True)                                       
    guids = ListField(EmbeddedDocumentField(Guid))

Then you can grab your Name and loop through its properties as nice object, remove and pop the Guids, then type Name.save()

0

精彩评论

暂无评论...
验证码 换一张
取 消