在使用新部署的启用身份验证的 MongoDB 容器设置新环境时,我遇到了以下异常:"An unhandled exception has occurred while executing the request. MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.NotSupportedException: Unable to create an authenticator."
就我而言,我使用的连接字符串如下例所示:mongodb://USER:PASSWORD@HOST:27017/?authMechanism=DEFAULT。该字符串在 MongoDB Compass 中工作得很好,但在我的 .NET 6.0 应用程序中却不行。
我能够使用以下代码成功插入新文档,但我无法获取新插入文档的_id.
插入后,user为null.谢谢!
MongoServer server = MongoServer.Create();
MongoDatabase test = server.GetDatabase("Test");
MongoCollection<BsonDocument> users = test.GetCollection("Users");
BsonDocument user = new BsonDocument();
user.Add("FirstName", "John");
user.Add("LastName", "Michael");
user.Add("Address", "123 Main Street");
user.Add("City", "Newport Beach");
user.Add("State", "CA");
user.Add("ZipCode", "92660");
user.Add("Email", "John.Michael@myemail.com");
user.Add("CreatedDate", DateTime.Now);
user.Add("IPAddress", "10.1.1.1");
user = users.Save(user);
string idSTring = user["_id"].ToString().Replace("\"", "");
Run Code Online (Sandbox Code Playgroud) 我们正在使用FluentMongo,现在已经在C#驱动程序中添加了LINQ支持,我们将删除对Fluent的依赖,并单独使用官方C#驱动程序.
有没有人这样做过,简单明了吗?有什么我们需要注意的吗?
我有以下结构:
public class ThreadDocument
{
public ThreadDocument()
{
Messages = new List<Message>();
Recipients = new List<Recipient>();
}
[JsonIgnore]
public ObjectId Id { get; set; }
public IList<Recipient> Recipients { get; set; }
public IList<Message> Messages { get; set; }
public DateTime LastMessageSent { get; set; }
public string LastSentByUserName { get; set; }
public string LastSentAvatarUrl { get; set; }
public string Snippet { get; set; }
public int MessageCount { get; set; }
}
public class Recipient
{
public …Run Code Online (Sandbox Code Playgroud) mongodb 10gen-csharp-driver mongodb-query mongodb-.net-driver
我正在尝试在MongoDB中映射一个私有支持字段.
我的模型看起来像:
public class Competitor
{
private IList<CompetitorBest> _competitorBests;
public virtual int CompetitorId { get; set; }
public virtual string Name
{
get
{
if (Type == "Team")
return TeamName;
return FirstName + " " + LastName;
}
}
public virtual IEnumerable<CompetitorBest> CompetitorBests
{
get { return _competitorBests.ToArray(); }
}
}
Run Code Online (Sandbox Code Playgroud)
我基本上试图将_competitorBests映射为CompetitorBests(存在于我的mongo文档中)
注意:这个模型由NHibernate共享(因此virtual)
我在文档中看不到任何明显的东西.
我该怎么做?
我目前正在将该[BsonRepresentation(BsonType.String)]属性应用于Guid我的域模型中的所有属性,以便以字符串格式序列化这些属性.除了令人厌倦之外,有时候这种情况并不常见,例如使用自定义Wrapper<T>类:
public class Wrapper<T>
{
public T Value { get; set; }
// Further properties / business logic ...
}
Run Code Online (Sandbox Code Playgroud)
当T是Guid时,Value属性将被存储为类型的二进制数据UuidLegacy(如将类型的任何属性Guid,这不是装饰与上述属性).但是,我希望所有Guids,包括Wrapper<Guid>.Value在数据库中表示为字符串.
有没有办法告诉MongoDB C#驱动程序以Guid字符串格式存储所有s?
一般计数查询将执行a
int count = collection.Find(filter).Count();
Run Code Online (Sandbox Code Playgroud)
现在按照过滤器加载所有记录,所以我说有100万条记录,其中有50万条与我的过滤器匹配,所以我的集合已经填充了0.5个文档.如果你想要这些文件,这已经足够了,但是如果你只是想知道计数而不是真的需要这些文件,那就好了.
我可以这样做吗?
int count = collection.Find(filter).SetLimit(1).Count();
Run Code Online (Sandbox Code Playgroud)
这给了我与第一个表达式相同的计数,但我希望内存不会被用作第一个表达式,帮助我知道找到"计数"而不加载所有文档的正确方法.谢谢.
我通过以下方式将GridFs函数与传统的mongo db c#驱动程序一起使用.
var file = Database.GridFS.FindOne(Query.EQ("_id", ObjectId.Parse(file.Id)));
Run Code Online (Sandbox Code Playgroud)
MongoDb C#2.1驱动程序,以另一种方式实现GridF,有人可以给我看一个例子吗?我还没找到任何文档.
我正在为使用mongoDB c#驱动程序的DAL创建一些单元测试。问题是我有要测试的方法:
public async virtual Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> predicate)
{
return (await Collection.FindAsync(predicate)).ToList();
}
Run Code Online (Sandbox Code Playgroud)
并使用Moq嘲笑了这样的集合:
var mockMongoCollectionAdapter = new Mock<IMongoCollectionAdapter<Entity>>();
var expectedEntities = new List<Entity>
{
mockEntity1.Object,
mockEntity2.Object
};
mockMongoCollectionAdapter.Setup(x => x.FindAsync(It.IsAny<Expression<Func<Entity,bool>>>(), null, default(CancellationToken))).ReturnsAsync(expectedEntities as IAsyncCursor<Entity>);
Run Code Online (Sandbox Code Playgroud)
但是,如果expectedEntities as IAsyncCursor<Entity>为null,则测试无法正常工作。
模拟此方法并处理IAsyncCursor的最佳方法是什么?
我刚刚将我的MongoDB从2.5.0更新到2.7.0.Visual Studio告诉我,当我想创建这样的索引时:
protected override Task OnPerformMaintenanceAsync(CancellationToken cancellationToken) =>
NotificationLogs.Indexes.CreateOneAsync(Builders<NotificationLog>.IndexKeys.Ascending(_ => _.TimestampUtc));
Run Code Online (Sandbox Code Playgroud)
已经过时了.他们希望我们使用CreateIndexModel https://mongodb.github.io/mongo-csharp-driver/2.6/apidocs/html/T_MongoDB_Driver_CreateIndexModel_1.htm
唯一的问题是我找不到一个能够实现同样功能的例子.
我试过了:
protected Task OnPerformMaintenanceTestAsync(CancellationToken cancellationToken)
{
// Old approach
// var builder = Builders<NotificationLog>.IndexKeys.Ascending(x => x.TimestampUtc);
// New approach
var indexModel = new CreateIndexModel<NotificationLog>(nameof(NotificationLog.TimestampUtc));
return NotificationLogs.Indexes.CreateOneAsync(indexModel);
}
Run Code Online (Sandbox Code Playgroud)
我一直得到以下异常:
System.FormatException: 'JSON reader was expecting a value but found 'TimestampUtc'.'