I'm new to C#/.NET and i've been trying to implement a database server (just for learning purposes) in a dictionary application where you can select words from a listbox and it will display the values in text boxes. I'm trying to get a list of only the keys called "Word" and "Translation" to strings
this is the bson(json) document
{
"_id": "5ca01f8e36601d012c7b2da1",
"Word": "Bonjour",
"Translation": "Hello",
"Word_Type": "Something",
"Synonyms": "Salut",
"Use_Cases": "Bonjour, comment vas-tu?",
"Definition": "It's a Greeting"
}
and this is the code i'm using to get the data from database:
private MongoClient client = new MongoClient("mongodb://localhost:27017");
/// <summary>
/// All the data needed for words
/// </summary>
public class DWord
{
public ObjectId _id { get; set; }
public string Word { get; set; }
public string Translation { get; set; }
public string Word_Type { get; set; }
public string Synonyms { get; set; }
public string Use_Cases { get; set; }
public string Definition { get; set; }
}
private void MethodThatGetsData()
{
var database = client.GetDatabase("DictDB");
var collection = database.GetCollection<BsonDocument>("Words");
var records = collection.Find(new BsonDocument()).ToList();
// I tried using foreach, but I don't know how to get the keys in that bsondocument (called "record" below)
foreach (string record in records)
{
WordsListBox.Items.Add(record);
}
}
I want to know how to get the key values for each record, thanks for any help
Replace your loop with something like this:
foreach (BsonDocument record in records)
{
IEnumerable<string> names = record.Names;
// Do something with the names...
}
Note, enumerating through records returns a BsonDocument for every find result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.