![](/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.