You can search for documents with missing fields by using a static index if it indexes the field which is
suspected to be missing in some of the documents.
The index definition must also index a field that exists in every document (such as
Id) so that all documents will be indexed.
- For example, if you want to find documents that are missing the field
Freight in the
query an index that indexes the fields
If your static index does not contain the desired field, either
- Modify your index definition to index the specific field. (This will trigger re-indexing.)
- Create an auto-index.
Example: Query a Static Index
In our example, we are looking for documents that are missing the field
Freight from the collection
First we need an index that includes
Freight and a field that exists in every document
We index the missing field
Freight and the field
Id, which exists in every document.
This way, the index includes all of the documents in the collection,
including those that are missing the specified field.
// Create or modify a static index called Orders_ByFreight
public class Orders_ByFreight : AbstractIndexCreationTask<Order>
// Specify collection name
Map = orders => from doc in orders
// Field that is missing in some documents
// Field that exists in all documents
Then we query the index to find documents where the field does not exist
Query the index
Orders_ByFreight and filter documents where
freight does not exist.
List<Order> results = session
// Query the static index
// Verify that the index is not stale (optional)
// Negate the next method
// Specify the field that is suspected to be missing
.WhereExists(x => x.Freight)
// `results` will contain the list of incomplete documents.
from index "Orders/ByFreight"
where true and not exists("Freight")
// `not` cannot come immediately after `where`, thus we use `where true`.
List<T> results = session
||An object in a collection (singular of the collection name - e.g. Order from the collection Orders).
||The name of the index that you want to use.
||The field that is missing in some of the documents.