Browsing the archives for the MongoDB tag.

MongoDB Wildcard Query

Examples

icon

MongoDB uses regex in certain parts of their query syntax. The following is an example of how to perform a wildcard query:

db.collectionName.find( { fieldName : /.*cnn.*/ } );

This example searches the collection for all documents with a field that contains, “%cnn%”. Of course in your query you need to replace “collectionName” with the name of your collection and “fieldName” with the field you are interested in.

1 Comment

MongoDB – Find Documents Missing Fields

Examples

icon

In MongoDB it is possible that some documents will not contain fields other documents have. The following code shows how to find these documents using the mongo shell.

db.collectionName.find("function() { return this.fieldName == null }")
db.collectionName.find("function() { return this.fieldName == null }").count()

The second example shows how to only get the count.

In this example. you would replace “collectionName” with the name of the collection you are interested in querying. Additionally, you would change “fieldName” to the name of the field that interests you.

No Comments