'Show list of all Dictionary Objects in MongoDB
Another speed bump on my way to trying to use a Dictionary with a WebAPI. I am trying to implement something with MangoDB and this is only my second time so I am not very knowledgeable here. I have set up a connection with the following code.
private readonly IMongoCollection<Dictionary<Guid, Order>> ordersCollection;
public MongoDBRepository(IMongoClient mongoClient)
{
IMongoDatabase database = mongoClient.GetDatabase(databaseName);
ordersCollection = database.GetCollection<Dictionary<Guid, Order>>(collectionName);
}
Now I have implemented a way to save as I have verified it in the VSC MongoDB extension. I used the following code.
public void CreateOrder(Order order)
{
var values = new Dictionary<Guid, Order>
{
{
Guid.NewGuid(), order
}
};
ordersCollection.InsertOne(values);
}
I've now added several Dictionaries to the DB and I want to get them all. The method in the controller I have says:
[HttpGet]
public Dictionary<Guid, API.Models.Order> GetOrders()
{
var data = repository.GetOrders();
return data;
}
The best I can figure then is that I need to implement something along the lines of:
public Dictionary<Guid, Order> GetOrders()
{
return ordersCollection.Find(new BsonDocument()).ToDictionary();
}
I did some searches and found https://mongodb.github.io/mongo-csharp-driver/2.0/apidocs/html/M_MongoDB_Bson_BsonDocument_ToDictionary.htm and I verified I have a using MongoDB.Bson but it doesn't appear. I am assuming it's because of the way I am utylizing orderCollection. I have found other questions on here using other methods but I only have access to InsertOne, InsertMany, Find, ReplaceOne... Any pointers would be appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
