我正在尝试从Mongo DB中检索单个字段集,我可以这样做吗?

时间:2018-04-17 06:52:08

标签: mongodb

我想从Mongo DB中只获取一个字段详细信息,可以这样做吗?

var coll = dbobj.GetCollection<BsonDocument>("getResList");
var sa = coll.FindAll().ToList();
ViewBag.dropvalue = sa;

这组代码将提供集合中的所有细节,但我想获得sigle字段的全部价值。

我的示例Mongo数据是:

{ 
            "_id" : ObjectId("5ac36369f999cb77fbf1e0dd"), 
            "campaignId" : "6QndK", 
            "href" : "https://api.getresponse.com/v3/campaigns/6QndK", 
            "name" : "deegrp", 
            "description" : null, 
            "isDefault" : "false", 
            "createdOn" : "2018-04-03T11:19:23+0000"
            }
            { 
             "_id" : ObjectId("5ad58dc84e0e4dfe295c8380"), 
             "campaignId" : "6Luex", 
             "href" : "https://api.getresponse.com/v3/campaigns/6Luex", 
             "name" : "deegrp2", 
             "description" : null, 
             "isDefault" : "false", 
             "createdOn" : "2018-04-17T06:01:33+0000"
             }

我想要检索每个人的'campaignId'

1 个答案:

答案 0 :(得分:0)

var query = Schema.find({}).select({'campaignId':1});

query.exec(function (err, data) {
    if (err) {
       console.log(err);
    }
    if(data.length>0){
       console.log(data);
    }
});