具有空值的MongoDb查询数组 [英] MongoDb query array with null values
问题描述
我的收藏夹中的对象看起来像:
I have objects in my collection that look like:
{ MyArray:[null, "some value", null] }
我想查询具有空值作为数组元素的那些对象.查询
I want to query those objects that have a null value as an array element. The query
db.collection.find({"MyArray":null})
不起作用,似乎只能找到MyArray为空的对象,例如
does not work, it seems to find only the objects with MyArray being null, e.g.
{ MyArray:null }
如何定义查询?
请看我的真实对象之一:
Please see one of my real objects:
{
"_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
]
}
然后我通过以下方式查询:
And then I query by:
db.collection.find({"Attrs":null})
没有结果.
推荐答案
好吧,我通过通过C#驱动程序加载空"候选对象来解决了这个问题,结果发现这些值不是null,而是Bson Undefined!它们在外壳中显示为"null"!请参阅 http://jira.mongodb.org/browse/SERVER-2426 . 这有效:
Ok, I figured it out by loading my "null" candidates via my C# driver and it turns out that the values are not null, but Bson Undefined!! They are displayed as "null" in the shell! See http://jira.mongodb.org/browse/SERVER-2426. This works:
db.xxx.find({"Attrs":{$type:6}})
这篇关于具有空值的MongoDb查询数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!