How can I use MongoDB to find all documents which have a field, regardless of the value of that field?

MongoDBDatabaseBig Data Analytics

<p>To use MongoDB to find all documents which have a field, regardless of the value of that field, use the $exists operator. Following is the syntax</p><pre class="prettyprint notranslate">db.yourCollectionName.find({yourFieldName:{$exists:true}});</pre><p>Let us create a collection with documents</p><pre class="prettyprint notranslate">&gt;db.findAllDocumentWhichHaveFieldDemo.insertOne({&quot;StudentName&quot;:&quot;John&quot;,&quot;StudentAge&quot;:null}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5c9d1d60a629b87623db1b22&quot;) } &gt;db.findAllDocumentWhichHaveFieldDemo.insertOne({&quot;StudentName&quot;:&quot;Larry&quot;,&quot;StudentAge&quot;:null}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5c9d1d70a629b87623db1b23&quot;) } &gt;db.findAllDocumentWhichHaveFieldDemo.insertOne({&quot;StudentName&quot;:&quot;Chris&quot;,&quot;StudentAge&quot;:&quot;&quot;}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5c9d1d7ba629b87623db1b24&quot;) } &gt;db.findAllDocumentWhichHaveFieldDemo.insertOne({&quot;StudentName&quot;:&quot;Robert&quot;,&quot;StudentAge&quot;:&quot;&quot;}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5c9d1d81a629b87623db1b25&quot;) }</pre><p>Following is the query to display all documents from a collection with the help of find() method</p><pre class="prettyprint notranslate">&gt; db.findAllDocumentWhichHaveFieldDemo.find().pretty();</pre><p>This will produce the following output</p><pre class="result notranslate">{ &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5c9d1d60a629b87623db1b22&quot;), &nbsp; &nbsp;&quot;StudentName&quot; : &quot;John&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : null } { &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5c9d1d70a629b87623db1b23&quot;), &nbsp; &nbsp;&quot;StudentName&quot; : &quot;Larry&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : null } { &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5c9d1d7ba629b87623db1b24&quot;), &nbsp; &nbsp;&quot;StudentName&quot; : &quot;Chris&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : &quot;&quot; } { &nbsp; &nbsp;&quot;_id&quot; : ObjectId(&quot;5c9d1d81a629b87623db1b25&quot;), &nbsp; &nbsp;&quot;StudentName&quot; : &quot;Robert&quot;, &nbsp; &nbsp;&quot;StudentAge&quot; : &quot;&quot; }</pre><p>Following is the query to use MongoDB to find all documents which have a field, regardless of the value of that field</p><pre class="prettyprint notranslate">&gt; db.findAllDocumentWhichHaveFieldDemo.find({StudentAge:{$exists:true}});</pre><p>This will produce the following output</p><pre class="result notranslate">{ &quot;_id&quot; : ObjectId(&quot;5c9d1d60a629b87623db1b22&quot;), &quot;StudentName&quot; : &quot;John&quot;, &quot;StudentAge&quot; : null } { &quot;_id&quot; : ObjectId(&quot;5c9d1d70a629b87623db1b23&quot;), &quot;StudentName&quot; : &quot;Larry&quot;, &quot;StudentAge&quot; : null } { &quot;_id&quot; : ObjectId(&quot;5c9d1d7ba629b87623db1b24&quot;), &quot;StudentName&quot; : &quot;Chris&quot;, &quot;StudentAge&quot; : &quot;&quot; } { &quot;_id&quot; : ObjectId(&quot;5c9d1d81a629b87623db1b25&quot;), &quot;StudentName&quot; : &quot;Robert&quot;, &quot;StudentAge&quot; : &quot;&quot; }</pre><p>Following is the query if you do not want the field &ldquo;StudentName&rdquo; in the result</p><pre class="prettyprint notranslate">&gt;db.findAllDocumentWhichHaveFieldDemo.find({},{StudentName:0},{StudentAge:{$exists:true}});</pre><p>This will produce the following output</p><pre class="result notranslate">{ &quot;_id&quot; : ObjectId(&quot;5c9d1d60a629b87623db1b22&quot;), &quot;StudentAge&quot; : null } { &quot;_id&quot; : ObjectId(&quot;5c9d1d70a629b87623db1b23&quot;), &quot;StudentAge&quot; : null } { &quot;_id&quot; : ObjectId(&quot;5c9d1d7ba629b87623db1b24&quot;), &quot;StudentAge&quot; : &quot;&quot; } { &quot;_id&quot; : ObjectId(&quot;5c9d1d81a629b87623db1b25&quot;), &quot;StudentAge&quot; : &quot;&quot; }</pre>
raja
Updated on 30-Jul-2019 22:30:25

Advertisements