I have a MongoDB collection called "employees" where there are documents like this:
{
"_id": "4dc077b07701540f34000001",
"FullName": "Fullname...",
"FirstName": "First name...",
"LastName": "Last name...",
"Title": "Title... ",
"Location": "Location...",
"Customer": "Customer...",
"Phone": "Phone...",
"CellPhone" : "Cellphone...",
"EMail": "E-mail..."
}
I am trying to make a method that does a foreach loop on all documents in the collection and puts each value of the FullName-key and adds it to a List.
List<string> listed = new List<string>();
IMongoCollection collection = _db.GetCollection("employees");
//var开发者_如何学运维 list = collection.FindAll().Documents.ToList();
var persons = from p in collection.AsQueryable()
select p["FullName"];
//foreach (var lt in list)
foreach (var name in persons)
{
//listed.Add(lt["FullName"].ToString());
listed.Add(name.ToString());
}
return listed;
Above is the code I have tried, the commented-code returns a NullReferenceException, the uncommented code returns the whole document in a list.
My question is: What kind of query must I do to get each value for every key called "FullName" into a list? I am using the current MongoDB stable version on Windows 7, and I am using the MongoDB-CSharp community driver. All help and pointers are appreciated!
var collection = _db.GetCollection("employees");
return (from p in collection.AsQueryable()
select p["FullName"]).toList();
Should do it?
However you should consider using the generic methods as seen in:
http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-GetCollectionmethod
精彩评论