标签: mongodb-.net-driver

如何在MongoDB中的FindOne中使用SetField用于C#驱动程序

我使用mongodb的官方C#驱动程序,我想使用像Find这样的FindOne查询中的SetFields.

var query = Query.EQ("Name", name);
Users.Find(query).SetFields(Fields.Exclude("Password"));
Run Code Online (Sandbox Code Playgroud)

是否可以这样做,因为FindOne返回一个实际的类而不是mongodb游标.

c# mongodb mongodb-.net-driver

8
推荐指数
1
解决办法
7988
查看次数

如何在Mongo驱动程序中为"orderby"编写查询以进行排序?

我正在尝试使用MongoDB的C#驱动程序从MongoDB中的"Deal"集合中检索最近的五个文档.我可以用下面的代码来做.

public IList<TEntity> GetRecentFive()
{
    IList<TEntity> entities = new List<TEntity>();
    using (MongoDbContext dbContext = new MongoDbContext(_dbFactory))
    {
        var cursor = dbContext.Set<TEntity>().FindAll().SetSortOrder(SortBy.Descending("ModifiedDateTime")).SetLimit(5);

        foreach (TEntity entity in cursor)
        {
            entities.Add(entity);
        }
    }

    return entities;
}
Run Code Online (Sandbox Code Playgroud)

但我想只获取最近的5个文档,FindAll()加载集合中的所有文档.我试图用Find()来做,但它需要一个查询作为参数.如何在Mongo驱动程序中为C#编写"orderby"查询进行排序?

/sf/answers/150393561/在这里问了一个类似的问题.但是接受的答案对我不起作用.

c# mongodb mongodb-.net-driver

8
推荐指数
2
解决办法
1万
查看次数

MongoDB - 覆盖C#基元类型的默认Serializer

我想将C#Doubles的表示形式更改为圆形Int64,并在MongoDB的序列化C#驱动程序堆栈中使用四位小数位移.换句话说,存储(双)29.99为(Int64)299900

我希望这对我的应用程序透明.我已经看过自定义序列化程序,但我不想覆盖所有内容,然后将带有回退的Type打开到默认值,因为这有点乱.

我可以看到RegisterSerializer()不允许我为现有类型添加一个,并且BsonDefaultSerializationProvider有一个原始序列化器的静态列表,它被标记为内部私有成员,所以我不能轻易地子类化.

我还可以看到代表双胞胎代表Int64是可能的,但这是演员而不是转换.我需要在两个序列化方向上基本上进行转换和转换.

我希望我可以给默认的序列化程序一个自定义序列化程序来覆盖它自己的一个,但这意味着一个肮脏的黑客.

我错过了一个非常简单的方法吗?

mongodb-.net-driver

8
推荐指数
2
解决办法
6828
查看次数

Mongo Vs Raven评估

我正在评估nosql数据库并最终确定了两个.RavenDB和MongoDb.哪一个更好,为什么?这将有助于编写更多面向性能的查询.

mongodb ravendb mongodb-.net-driver

8
推荐指数
1
解决办法
7648
查看次数

如何在c#foreach循环中使用MongoDB的Query和QueryBuilder?

我正在尝试查询我的收藏品,但我不确定如何对其进行"追加" Query.And()

这是我创建Item文档的域模型:

public class Item
{
    public ObjectId Id { get; set; }
    public string ItemTypeTemplate { get; set; }
    public string UsernameOwner { get; set; }

    public IList<ItemAttribute> Attributes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

IList<ItemAttribute>根据收集的变化ItemTypeTemplate(某种查找关键项目的属性预先确定的名单)

以下是Item文档示例:

{
    "_id" : ObjectId("5130f9a677e23b11503fee72"),
    "ItemTypeTemplate" : "Tablet Screens", 
         //can be other types like "Batteries", etc.
         //which would change the attributes list and values
    "UsernameOwner" : "user032186511",
     "Attributes" : [{
         "AttributeName" : "Screen Size",
         "AttributeValue" …
Run Code Online (Sandbox Code Playgroud)

c# mongodb mongodb-.net-driver

8
推荐指数
1
解决办法
2万
查看次数

MongoDB C# - 如何将任意JSON文档保存为动态类型?

我正在尝试编写一个通用的Web Api控制器,它允许我将JSON文档保存到集合中而不指定C#类型.我试图将代码压缩到基本要素:

public class PassThroughController : ApiController
{
    [Route("api/mongodb/{collection}")]
    public void Post(string collection, dynamic document)
    {
        const string connectionString = "mongodb://localhost";

        var client = new MongoClient(connectionString);
        var db = client.GetServer().GetDatabase("SampleDb");
        var mongoCollection = db.GetCollection(collection);

        mongoCollection.Save(document,
            new MongoInsertOptions
            {
                WriteConcern = WriteConcern.Acknowledged
            });
        }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试发布一个简单的文档:

{ id: "2112", name: "Rush" }
Run Code Online (Sandbox Code Playgroud)

但无论我发送给方法的是什么,我都会收到与此类似的错误:"保存只能用于具有Id的文档."

我们尝试了Id(Id,id,_id)的许多不同属性,但它们都会导致类似的问题.

有任何想法吗?

谢谢

mongodb-.net-driver

8
推荐指数
2
解决办法
8546
查看次数

C#驱动程序2.0 Mongodb UpdateOneAsync

`

public class Student
{
    public long StudentId {get; set;}
    public string Fname {get; set;}
    public string Lname {get; set;}
    public List<ObjectId> CoursesList {get; set;}
    public int IQ {get;set;}
}

public class Courses
{
    [BsonId]
    public ObjectId Id { get; set; }
    public string CourseNumber{get; set;}
    public string CourseName{get; set;}
}
Run Code Online (Sandbox Code Playgroud)

`

如何在Student对象的课程列表(第一次可能为null)中添加/附加courser Id

PS:我知道如何使用下面的命令设置字段.我希望它与上述问题类似

await StudentCollection.UpdateOneAsync(a => a.StudentId == studentId, Builders<Student>.Update.Set( a => a.IQ,90));
Run Code Online (Sandbox Code Playgroud)

c# mongodb mongodb-.net-driver

8
推荐指数
1
解决办法
1万
查看次数

MongoDB C#2.0 TimeoutException

我们最近将我们的Web应用程序升级到MongoDB C#Driver 2.0并部署到生产中.低于一定负载,应用程序运行正常.一旦生产服务器上的负载超过某个限制,应用程序的CPU立即降至0,大约30秒后,将多次记录此异常:

System.TimeoutException message: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = System.Collections.Generic.List`1[MongoDB.Driver.TagSet] } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", Type : "Standalone", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/10.4.0.113:27017" }", EndPoint: "Unspecified/10.4.0.113:27017", State: "Disconnected", Type: "Unknown" }] }.
stack trace:
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.<WaitForDescriptionChangedAsync>d__18.MoveNext() …
Run Code Online (Sandbox Code Playgroud)

timeoutexception mongodb-csharp-2.0 mongodb-.net-driver

8
推荐指数
2
解决办法
1万
查看次数

使用C#中的MongoClientSettings初始化MongoClient

我正在尝试从Mongo 2.0驱动程序初始化MongoClient,如下所示:

MongoClientSettings settings = new MongoClientSettings();
settings.WaitQueueSize = int.MaxValue;
settings.WaitQueueuTimeout = new TimeSpan(0,2,0);
settings.MinConnectionPoolSize = 1;
settings.MaxConnectionPoolSize = 25;
settings.Server = new MongoServerAddress("mongodb://localhost");
client = new MongoClient(settings)
Run Code Online (Sandbox Code Playgroud)

但是,当我现在尝试使用此代码插入文档时:

db = client.GetDatabase("local");
col = db.GetCollection<BsonDocument>(collectionName);
col.InsertOneAsync(new BsonDocument().Add(new BsonElement("id",BsonValue.Create(1)))).Wait();
Run Code Online (Sandbox Code Playgroud)

它没有做任何事情.它没有插入,也没有错误消息(虽然一段时间后输出中出现System.Timeout的第一次机会异常).如果我初始化客户端

client = new MongoClient("mongodb://localhost")
Run Code Online (Sandbox Code Playgroud)

它确实有效,并按预期上传文档.

我希望客户端能够处理非常高的写入吞吐量,所以我首先尝试了这些设置.我是否设置了一些错误的设置或是否存在其他问题?

编辑:经过一些更多的测试,它确实是我得到的System.Timeout异常.

.net c# mongodb mongodb-csharp-2.0 mongodb-.net-driver

8
推荐指数
1
解决办法
7580
查看次数

使用MongoDB C#驱动程序在嵌套数组上使用过滤器构建器进行查询

考虑存储为文档的以下对象结构:

public class Foo
{
    public string Id { get; set; }
    public ICollection<FooBar> Bars { get; set; }

    // ...
}

public class FooBar
{
    public string BarId { get; set; }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

使用带司机LINQ风格的查询,我可以Find全部Foo包含一个FooBar BarId这样的:

var foos = await m_fooCollection.Find( f => f.Bars.Any( fb => fb.BarId == "123") ).ToListAsync();
Run Code Online (Sandbox Code Playgroud)

如何使用FilterDefinitionBuilder而不是内联LINQ来实现相同的查询Find

c# mongodb mongodb-query mongodb-csharp-2.0 mongodb-.net-driver

8
推荐指数
1
解决办法
1万
查看次数