Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>AFAIK, there isn't a way and it is logical for it to be so.</p> <p>MongoDB being schema-less allows a single collection to have a documents with different fields. So there can't really be a description of a collection, like the description of a table in the relational databases.</p> <p>Though this is the case, most applications do maintain a schema for their collections and as said by Chris this is enforced by your application.</p> <p>As such you wouldn't have to worry about first fetching the available keys to make a query. You can just ask MongoDB for any set of keys (i.e the projection part of the query) or query on any set of keys. In both cases if the keys specified exist on a document they are used, otherwise they aren't. You will not get any error.</p> <p>For instance (On the mongo shell) :</p> <p>If this is a sample document in your <code>people</code> collection and all documents follow the same schema:</p> <pre><code>{ name : "My Name" place : "My Place" city : "My City" } </code></pre> <p>The following are perfectly valid queries :</p> <p>These two will return the above document :</p> <pre><code>db.people.find({name : "My Name"}) db.people.find({name : "My Name"}, {name : 1, place :1}) </code></pre> <p>This will not return anything, but will not raise an error either :</p> <pre><code>db.people.find({first_name : "My Name"}) </code></pre> <p>This will match the above document, but you will have only the default "_id" property on the returned document.</p> <pre><code>db.people.find({name : "My Name"}, {first_name : 1, location :1}) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload