About Search In…
Search In… or Value Search is a wide-range text search on the values stored in a MongoDB collection, database, or server. By default, it scans all documents for text matches (or regular expression matches) in all field values and field names. Search In… scans embedded documents and arrays and shows you the path to the matched field or name.
Starting Search In…
To open a Search In… view, select a collection, database, or server in the Connection Tree in Studio 3T. Then either select File > Search In… or right-click on the selected item and choose Search In….
The Search In… view will open. The tab title reflects where any future search takes place.
Below that are three indicators. The first shows the server in use. The second is a selector for which database to search. The third is a selector for which collection, within that database, to search. If you select a server for your search, the second selector will default to <all databases> and the collection selector will be set to <all collections>.
Selecting a database when opening Search In…, or adjusting the database selector to a particular database, will enable the collections selector.
This restricts the search to the selected database, but can still search <all collections> within that database.
Selecting a collection when opening Search In…, or adjusting the Collection selector to a particular collection, will further restrict the search to that particular collection.
Performing a Search
Once you have selected the scope of your Search In.. search, you can click on the Search type selector whether you want to search field values and names, just values, or just names. Then you can enter a search term in the search field. The search term can be plain text or a regular expression.
If you wish the search to be case-sensitive, check the Match case field to toggle case-sensitive searches on.
Check the RegEx field to toggle the use of regular expressions if you want to use a regular expression as a search term. Search In… regular expressions use the Java regular expression engine. Refer to Regular Expressions In Studio 3T for a reference to the formats of regular expression accepted.
We will examine the Advanced Options toggle later in this article.
The search term is, by default, a match with plain text. The text will match anywhere in a value. For many use cases, this is sufficient to home in on relevant fields. Use regular expressions where you want more defined precision in your matches.
Value Conversion
Note that when searching, values in the database documents which are not string types are converted to strings. Names are always strings and searches as strings, so the conversion does not apply to them.
For example, numbers present as strings so a Search In… search for 10 as plain text would match with 10, 100, 90.10 stored as numbers. It would also match with 10/6/22, a stored date converted to a string, as well as K100 (a string which contains a number).
Plain Text searches will always simply match the sequence of characters.
Viewing Search In… Results
Pressing the Return key in the search field or clicking on the Search button begins the search. You can follow how the search is doing in the lower left of the view, where a progress meter and count of matches is shown. Also in the lower left is a “cancel” button (red circle with cross) which can terminate the search early.
Each match is displayed in the five-column table. Whilst you can interact with the table as the search runs, the search term controls are only enabled when the search is complete. You will have to stop the search, or wait for it to finish, before you can adjust those settings.
Each row is made up of five elements, each one related to a match found by Value Search:
Database: The name of the database where this match is.
Collection: The name of the collection where this match is.
_id: The _id field of the document where the match is.
Path: The path, from the document’s root, to the field where the match is. Paths display in Studio 3T format. This is a field name that is optionally followed by a > and either a number or another field name. A number usually represents the index value of an array. This repeats as needed.
Value: The value of the field in which a match is found.
The Database, Collection, _id, and Path allow you to locate precisely where the value match is.
The particular parts of field value or name that match are shown with a yellow background.
Viewing Matched Documents
For any match displayed in Search In…, you have two options.
- Double-clicking the row will open the document containing the match in the Document JSON Viewer. You can only view the document here.
- Right-clicking on the row will display the context menu for the row.
View Document, like double-clicking, opens the document in the collection using the Document JSON Viewer.
Open in Collection Tab will open a new Collection Tab with a query that selects only the matched document. From the Collection Tab, you can edit fields inline, or view and edit the matched document.
Advanced Options
Advanced options enable an extra level of control over how Search In… examines collections.
Before scanning any collection’s documents, Search In… runs a query which will select the documents it searches. By default, this is an empty query which returns all documents in the collection. The Advanced options let you control what that query is. This allows you to focus search on specific fields, reduce the number of documents examined, or pre-sort the results.
You can create queries using the Collection Tab and Visual Query Builder. They can then be copied and pasted, or saved and loaded, into the Advanced options.
Query
The Query field takes a MongoDB query document specification. It is a pre-filtering stage for the documents in the value search. For example, if you only want to search documents where the country field is not “United States”, then you can set the query to:
{ “country”: { $ne: “United States” } }
Any document where the country field is not “United States” will move on to the Search In… search. Note that in this example, the result of the query would also return documents that did not have a country field at all (because null is not equal to United States). When doing a Search In… search with a query on multiple collections, remember that the same query will run on all the collections. If the query fails to match any documents – for example, because a field is being present in a collection – no fields in that collection will go on to be in the Search In… search.
Projection
The Projection field is useful in reducing the number of fields that pass into the Search In… search. By selecting fields for inclusion or exclusion, you can make sure that Search In… is retrieving fewer documents, but still thoroughly searching the rest of the fields. For example, if your collections have a large text record attached called notes which you don’t want searched, setting the Projection to { notes: 0 } will exclude that one field from the Search In… search. Again, this will be applied to all collections in scope.
Skip and Limit
Skip and Limit will, as they do in the Collections Tab and other queries, skip over a number of documents and then limit the number of documents returned by the query. If you want to only search 1000 documents in a collection, starting after the first 2000 documents, then setting Skip to 2000 would skip the first 2000 documents and setting Limit to 1000 would restrict the number of returned documents to a maximum of 1000.
Sort
Use the Sort field in Advanced options if you want the results to appear in a particular order. This will pre-sort the documents before they are passed on to the Search In… search. Again, it takes the same Sort specification as used in the Collections Tab query bar and Visual Query Builder.
That is a JSON list of field names with the values -1 or 1 to determine ascending or descending sorting for that field. Setting the Sort field to { birthdate: 1, username: 1} will sort the documents by ascending birthdate field value and ascending username text field value.
Here, we are doing a Search In… search that focusses on just the name and username fields, with a window that scans 50 documents (after skipping ten) from a collection sorted by birthdate and username. As you can see, this limits the results.