2017-09-14 109 views
1

由於MongoEngine says in documentation:爲什麼在MongoEngine中排除()不會排除所有字段?

只(*域)負載只有這個文件的域的子集。

後= BlogPost.objects(...)只( '標題', 'author.name')

現在我對我自己的用戶採集運行下同:

user = User.objects(__raw__=condition).only('status').get() 

但是,當我打印輸出(print user.to_json())它給像下面的結果:

{"_id": {"$oid": "59ba3c7428999b08223dc4aa"}, "cover": "", "avatar": "", "dob": {"$date": 1505347200000}, "created_at": {"$date": 1505384574064}, "updated_at": {"$date": 1505384574064}, "bio": "", "location": "", "account_type": "regular", "status": "inactive", "phone": ""} 

我不認爲我已經在這裏錯過了一些東西。

現在我用排除列入黑名單的某些字段:

user = User.objects(__raw__=condition).exclude('status').exclude('username').exclude('bio').exclude('email').get() 

輸出是:

{"_id": {"$oid": "59ba3c7428999b08223dc4aa"}, "fullname": "Mu Fullname", "password": "$pbkdf2-sha256$16366$y9lbq/Ueo5QSohSi1FrLmQ$YOLu.KzmLiDhS6BVh4cTPg7xFpYLZ/2l478jnROj0ok", "cover": "", "avatar": "", "dob": {"$date": 1505347200000}, "created_at": {"$date": 1505377384359}, "updated_at": {"$date": 1505377384359}, "bio": "", "location": "", "verification": {"email": false, "celebrity": false, "phone": false}, "account_type": "regular", "status": "inactive", "phone": ""} 

usernameemail被排除,但不statusbio

我的用戶模型爲:

class Verification(EmbeddedDocument): 
    email = BooleanField(default=False, allow_blank=True, allow_null=True) 
    celebrity = BooleanField(default=False, allow_blank=True, allow_null=True) 
    phone = BooleanField(default=False, allow_blank=True, allow_null=True) 


class User(Document): 
    email = EmailField(required=True, max_length=200, unique=True) 
    # regex '^\w+$' equals [a-zA-Z_] 
    username = StringField(regex='^\w+$', required=True, max_length=15, min_length=3, unique=True) 
    fullname = StringField(required=True, max_length=20) 
    password = StringField(required=True) 
    cover = StringField(default='') 
    avatar = StringField(default='') 
    dob = DateTimeField(default='') 
    created_at = DateTimeField(default=datetime.datetime.utcnow()) 
    updated_at = DateTimeField(default=datetime.datetime.utcnow()) 
    bio = StringField(default='') 
    location = StringField(default='') 
    verification = EmbeddedDocumentField(Verification, required=True) 
    account_type = StringField(required=True, default='regular', choices=('page', 'regular')) 
    status = StringField(required=True, default='inactive', choices=('inactive', 'active', 'block')) 
    phone = StringField(regex='^$|^[0-9()\\-\\.\\s]+$', default='') 

回答

1

看來,在案例文檔領域的exclude返回默認值,他們被排除:

Exclude in mongoEngine