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})
does not work, it seems to find only the objects with MyArray being null, e.g.
{ MyArray:null }
How do I need to define my query?
EDIT: 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})
which yields no results.
MongoDB fetch documents containing 'null' If we want to fetch documents from the collection "testtable" which contains the value of "interest" is null, the following mongodb command can be used : >db. testtable. find( { "interest" : null } ).
Solution 1: In case preservation of all null values[] or null fields in the array itself is not necessary. Filter out the not null elements using $filter , the ( all null elements) array would be empty, filter that out from documents using $match then $sort on values .
Indeed, it's not possible to store null values in a MongoDB document using a DataFrame. The Python None values are considered as missing attributes accordingly to this NoSQL specific allowance. However, if your column is numerical, you can force writing a null value by setting it to NaN.
In order to find documents with arrays, with null elements please run:
db.collection.find({"keyWithArray":{$elemMatch:{"$in":[null], "$exists":true}}})
According to safaribooksonline proper null
matching is performed using $in
(because you cannot use $eq
with null
).
Also, comparing with null
:
{"something":null}
will match documents with "something" field set to null and every document which does not have "something" field at all. Thus we have to make sure the key exists using $exists
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With