![](/img/trans.png)
[英]How can I replace an object in mongodb without specifying a filter using the C# driver?
[英]How to use MongoDB C# Driver without specifying a class
我正在使用MongoDB c#driver 2.0。 我試圖在沒有指定類型或類的情況下獲取集合。 注意:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core;
using MongoDB.Driver.Linq;
using MongoDB.Shared;
namespace Meshplex.Service.DataWarehouse
{
public class ProfileControllerMongoDB
{
private IMongoDatabase _mongoDb;
private IMongoCollection _myCollection;
//private IMongoCollection<ClassHere> _myCollection;
public ProfileDataControllerMongoDB()
{
_mongoDb = GetMongoDatabase();
_myCollection = _mongoDb.GetCollection(GetCollectionName());
//_myCollection = _mongoDb.GetCollection<ClassHere>("Collection");
}
public async Task<string> GetAllRecords()
{
//This should return json
return await _myCollection.Find(new BsonDocument());
}
如你所見,我應該在聲明IMongoCollection
時指定一個類。 有沒有辦法在不指定類的情況下使用MongoDB驅動程序?
MongoDb支持泛型參數中的dynamic
類型。
IMongoCollection<dynamic> _myCollection = _mongoDb.GetCollection<ClassHere>("Collection");
見http://mongodb.github.io/mongo-csharp-driver/2.0/what_is_new/#new-api
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.