Want a sub document which is present in document, from MongoDB Database


I have a Json data stored in my MongoDB database as showing below,

        "_id" : ObjectId("5f17109a9013adc57942af2a"),
        "name" : "Demo Name",
        "dob" : "04/01/2000",
        "phone" : "1234567890",
        "email" : "demoname@gmail.com",
        "password" : "0987",
        "cPass" : "0987",
        "location" : "Civil line, Mumbai"

And I only want an name (sub document) i.e Demo Name here from this whole document:

: Demo Name 

I’m writing this query:

var employ = await coll.findOne( { "email": "demoname@gmail.com" } );

But it gives me whole document in var employ. I only want name in var employ. How can I do that with Mongo Dart.


This query finds the element whose email value is matched with "demoname@gmail.com" and after finding element, It is going to exclude the fields i.e _id, dob, phone, email, password, cPass and location. The value remains is "Demo Name" along with it’s key.

 var employ = await coll
              .find(where.match("email", "demoname@gmail.com").excludeFields([

For removing key we have to use the following code.

var arrayEle = employ[0];          // This statement can store the value in arrayEle from zeroth position.
var empName = arrayEle['name'];    // This statement can remove the key "name" and only value of the key i.e "Demo Name" gets stored in empName variable.   
  print(empName);                  // This statment print the value stored in empName which is "Demo Name".

Answered By – yash_412

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *