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

1 Comment

  1. dm  •  Sep 18, 2009 @23:01

    FYI the query optimizer is smart about regex’s of the form /^/ — analogous to LIKE “<normal_chars"%" in mysql.

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>