I'm having a hard time understanding why the following happens within mongodb when dealing with nulls and array indexes within queries.
Say we have the following:
> db.test.find()
{ "_id" : ObjectId("5852da24507d8c27f4e3c357"), "item" : null }
{ "_id" : ObjectId("5852da2d507d8c27f4e3c358"), "item" : { "something" : true } }
{ "_id" : ObjectId("5852da33507d8c27f4e3c359") }
When I try to find items that are null I get ObjectId("5852da24507d8c27f4e3c357") and ObjectId("5852da33507d8c27f4e3c359") which is as expected based on https://docs.mongodb.com/v3.2/tutorial/query-for-null-fields/
> db.test.find({"item" : null})
{ "_id" : ObjectId("5852da24507d8c27f4e3c357"), "item" : null }
{ "_id" : ObjectId("5852da33507d8c27f4e3c359") }
But when I try to do the same but within a array with a given index, It's completely different set of results:
Say we have the following:
> db.test.find()
{ "_id" : ObjectId("5852dbe1507d8c27f4e3c35c"), "a" : [ { "something" : true }, { "something" : true }, null ] }
{ "_id" : ObjectId("5852dbf4507d8c27f4e3c35d"), "a" : [ { "something" : true }, { "something" : true }, { "something" : true } ] }
{ "_id" : ObjectId("5852dbfb507d8c27f4e3c35e"), "a" : [ { "something" : true }, { "something" : true } ] }
But If I do the query based on the index of 2 is null, we still get returned all the documents:
> db.test.find({"a.2": null})
{ "_id" : ObjectId("5852dbe1507d8c27f4e3c35c"), "a" : [ { "something" : true }, { "something" : true }, null ] }
{ "_id" : ObjectId("5852dbf4507d8c27f4e3c35d"), "a" : [ { "something" : true }, { "something" : true }, { "something" : true } ] }
{ "_id" : ObjectId("5852dbfb507d8c27f4e3c35e"), "a" : [ { "something" : true }, { "something" : true } ] }
This to me doesn't seem like its working as expected? But we can however see that the index position works correctly as we can do the following query:
> db.test.find({"a.2": {something:true}})
{ "_id" : ObjectId("5852dbf4507d8c27f4e3c35d"), "a" : [ { "something" : true }, { "something" : true }, { "something" : true } ] }
Is this a bug within mongodb and dealing with arrays and nulls?