17

My collection (MongoDB v 2.0.2) has following records:

db.organization.find({})
{ "_id" : 1001, "path" : [ ], "parent" : null }
{ "_id" : 1002, "path" : [ 1001 ], "parent" : NumberLong(1001) }

organization has indexes:

db.organization.ensureIndex({"path":1});
db.organization.ensureIndex({"parent":1},{sparse:false});

(note I put awarnes sparse : false - to grant that null is indexed) But, executing:

db.organization.find({"parent":null})

Returns empty set. What is wrong? Thank you in advance

2
  • Are you still running into issues with this? Can you give the output of db.system.indexes.find() as well as db.organization.find().explain()? Commented Feb 21, 2012 at 22:23
  • @Barrie, right now I cannot reproduce this error. I'm pretty sure that it was reached by multiple applying of js scripts from command line tool. Those time I've started explain to recognize error, but it shown normal usage of indexes Commented Mar 5, 2012 at 16:12

2 Answers 2

19

I had the same issue. After reading the following documents

I tried to query for the different BSON element types and found that my null was represented as a BSON element type 6 (undefined, deprecated) instead of the expected BSON element type 10 (null).

db.collection.find({ field: { "$type" : 6} };
Sign up to request clarification or add additional context in comments.

5 Comments

based on the first link, I think it should be db.collection.find({ field: { "$type" : 10} };
@AbdelHady: Perhaps the documents represent a simplified version of reality. I just had to use { field: { $type: 6 } } in an update where I really want to say { field: null }.
@AbdelHady: Scratch that, I think { $type: 10 } really is the right thing and I'm just confusing myself.
But I tried it myself in my database, {field: null} gave the same result as {field: { "$type" : 10} } but {field: { "$type" : 6} } didn't. And also, the first link in the answer here states that "The { cancelDate : { $type: 10 } } query matches documents that contains the cancelDate field whose value is null only;"
to find null bson types as @Abdel Hady said, use db.collection.find({ field: { "$type" : 10} };
6

Just checked following script at 2.0 and 2.0.2:

db.items.insert({ "_id" : 1001, "path" : [ ], "parent" : null })
db.items.insert({ "_id" : 1002, "path" : [ 1001 ], "parent" : NumberLong(1001) })
db.items.ensureIndex({"path":1});
db.items.ensureIndex({"parent":1},{sparse:false});
db.items.find({"parent":null})

actually returns one document that you expect:

{ "_id" : 1001,
  "path" : [],
  "parent" : null } 

Also you can look into this doc about querying and nulls, probably should help you avoid possible future mistakes.

1 Comment

it is some magic. You example with items works, but mine sucks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.