0

我有一个 Json 数据存储在我的 MongoDB 数据库中,如下所示,

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

我只想要一个名称(子文档),即整个文档中的演示名称:

: Demo Name 

我正在写这个查询:

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

但它给了我在varemploy中的整个文档。我只想要 var 雇佣中的名字。我怎么能用 Mongo Dart 做到这一点。

4

1 回答 1

0

此查询查找其电子邮件值与“demoname@gmail.com”匹配的元素,在找到元素后,它将排除字段,即_id、dob、电话、电子邮件、密码、cPass 和位置。剩下的值是“Demo Name”以及它的键。

 var employ = await coll
              .find(where.match("email", "demoname@gmail.com").excludeFields([
                "_id",
                "dob",
                "phone",
                "email",
                "password",
                "cPass",
                "location",
              ]))
              .toList();

要删除密钥,我们必须使用以下代码。

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".
于 2020-07-27T04:50:44.987 回答