Overview of Filter Operator $id

Other filer operators generally look for particular JSON fields within documents and try to match their values. Operator $id is an exception in that it instead matches document keys. It thus matches document metadata, not document content. You use operator $id in the outermost condition of a filter.

Example 2-4 shows three filter that use operator $id.

Example 2-4 Using $id To Find Documents That Have Given Keys

Find the unique document that has key key1.

{"$id" : "key1"}

Find the documents that have any of the keys key1, key2, and key3.

{"$id" : ["key1","key2","key3"]}

Find the documents that have at least one of the keys key1 and key2, and that have an object with a field address.zip whose value is at least 94000.

{"$and" : [{$id : ["key1", "key2"]},
           {"address.zip" : { "$gte" : 94000 }}]}

Related Topics