需要一些幫助與基於子文檔屬性的值,但也抑制該子文檔屬性出現在結果的蒙戈查詢。MongoDB的子文檔投影
這裏是我的用戶對象:
{
"username" : "abc",
"emails" : [
{
"address" : "[email protected]",
"default" : true
},
{
"address" : "[email protected]",
"default" : false
}
]
},
{
"username" : "xyz",
"emails" : [
{
"address" : "[email protected]",
"default" : false
},
{
"address" : "[email protected]",
"default" : true
}
]
}
我的目標是讓下面的輸出(用「emails.default」:真實的,但沒有出現在結果中「emails.default」屬性):
{
"username" : "abc",
"emails" : [
{
"address" : "[email protected]",
}
]
},
{
"username" : "xyz",
"emails" : [
{
"address" : "[email protected]",
}
]
}
使用$位置操作:
collection.find({"emails.default":true}, {"username":1,"emails.$":1})
我得到正確的電子郵件子文檔AP梨,但我仍然得到「emails.default」屬性回:
{
"username" : "abc",
"emails" : [
{
"address" : "[email protected]",
"default" : true
}
]
},
{
"username" : "xyz",
"emails" : [
{
"address" : "[email protected]",
"default" : true
}
]
}
出於某種原因,當我使用這個說法:
collection.find({"emails.default":true}, {"username":1,"emails.address":1})
我得到以下結果(好像查詢部分聲明被忽略)
{
"username" : "abc",
"emails" : [
{
"address" : "[email protected]",
},
{
"address" : "[email protected]",
}
]
},
{
"username" : "xyz",
"emails" : [
{
"address" : "[email protected]",
},
{
"address" : "[email protected]",
}
]
}
幫助非常感謝,在此先感謝。