我的收藏中的对象看起来像:
{ MyArray:[null, "some value", null] }
Run Code Online (Sandbox Code Playgroud)
我想查询具有null值的对象作为数组元素.查询
db.collection.find({"MyArray":null})
Run Code Online (Sandbox Code Playgroud)
不起作用,它似乎只找到MyArray为null的对象,例如
{ MyArray:null }
Run Code Online (Sandbox Code Playgroud)
我如何定义查询?
编辑:请看我的一个真实的对象:
{
"_id" : BinData(3,"ch9PrWveqU6niB6FGVhIOg=="),
"PeerGroup" : "male",
"ProductId" : BinData(3,"weRiKOtXEUSDZHkGHLcwzw=="),
"CategoryIds" : [
BinData(3,"BXzpwVQozECLaPkJy26t6Q=="),
BinData(3,"ox303ZeM50KelvoUbPBJ8Q=="),
BinData(3,"26ziaY+G9UKMyjmtVkkhcg=="),
BinData(3,"D2X8vObte0eJHNcDfp2HBw==")
],
"ShopId" : BinData(3,"ZdfPmrlKR0GkLPC4djJuKw=="),
"BrandId" : BinData(3,"kCHyuyLvgECxPF1nxwr7qQ=="),
"Created" : ISODate("2012-08-24T07:42:12.416Z"),
"LastActivity" : ISODate("2013-01-14T19:38:11.776Z"),
"Price" : 129.9,
"Sale" : false,
"Rating" : 11.057340703605368,
"RatingTimed" : 0.05670431130054035,
"Available" : null,
"FreeDelivery" : null,
"Attrs" : [
null,
null,
null,
null
]
}
Run Code Online (Sandbox Code Playgroud)
然后我通过以下方式查询:
db.collection.find({"Attrs":null})
Run Code Online (Sandbox Code Playgroud)
没有结果.
Kry*_*ian 29
要查找包含数组的文档,请使用null元素运行:
db.collection.find({"keyWithArray":{$elemMatch:{"$in":[null], "$exists":true}}})
Run Code Online (Sandbox Code Playgroud)
据safaribooksonline适当的null
匹配,进行使用$in
(因为你不能用$eq
同null
).另外,与null
:
{"something":null}
Run Code Online (Sandbox Code Playgroud)
将匹配"something"字段设置为null的文档和每个没有"something"字段的文档匹配.因此,我们必须确保密钥存在使用$exists
.
Max*_*Max -1
好吧,我通过 C# 驱动程序加载“null”候选者来解决这个问题,结果发现这些值不是 null,而是 Bson Undefined!它们在 shell 中显示为“null”!请参阅http://jira.mongodb.org/browse/SERVER-2426。这有效:
db.xxx.find({"Attrs":{$type:6}})
归档时间: |
|
查看次数: |
9041 次 |
最近记录: |