我使用的是用于C#的2.2版MongoDB驱动程序.我想对查询进行分页:对查询的响应必须包含当前页面的项目以及与查询匹配的项目总数.
我想做一个查询.使用mongo shell我可以意识到:
var c = db.mycol.find({....}).skip(0).limit(10)
var total = c.count();
while (c.hasNext()) {
print(tojson(c.next()));
}
Run Code Online (Sandbox Code Playgroud)
但是使用C#驱动程序,我不知道如何只使用一个查询.
var find = collection
.Find(x => x.Valid == true)
.Skip(0)
.Limit(10);
var cursor = await find.ToCursorAsync(cancellationToken);
// How to get the count? There is no method in the IAsyncCursor interface.
Run Code Online (Sandbox Code Playgroud)
可能吗 ?一些想法?
好的,我认为这将是一个询问内容管理系统的地方.
基本上,我运行一个网站www.com,没有人更新它,因为它太复杂了.有什么方法可以使用像Wordpress或Joomla这样的CMS来更新某些DIV,这样我就不必重新设计整个网站了吗?或者有没有人会推荐的系统?
我想在 MongoDb 查询中获得第一个前 10 个结果
为此我尝试了这样的事情
return _users.Collection.FindAll().Limit(10).ToList();
Run Code Online (Sandbox Code Playgroud)
但这不起作用,然后我根据大多数资源尝试了以下操作,但这也不起作用
return _users.Collection.Aggregate([{ "$limit": 10 }]).ToList();
Run Code Online (Sandbox Code Playgroud) public async Task SomeWork()
{
Debug.WriteLine("SomeWork for User : " + Context.User.Identity.Name);
var userProfile = await UserProfileCollection.Find(u => u._id == Context.User.Identity.Name).FirstOrDefaultAsync();
userProfile.SuccessfullConnect = true; // currently false
var up = await UserProfileCollection.FindOneAndReplaceAsync(u => u._id == userProfile._id, userProfile);
Debug.WriteLine(ObjectDumper.Dump(userProfile));
Debug.WriteLine(ObjectDumper.Dump(up));
Debug.WriteLine("Saved SomeWork for User : " + Context.User.Identity.Name);
}
Run Code Online (Sandbox Code Playgroud)
以下是调试输出。我的文档没有被替换。检查SuccesslConnect变量,它仍然为false。我在服务器上检查了文件也没有替换。有时它工作,有时不工作。
SomeWork for User : +919933221101
{TestSignalR_Server.Models.UserProfile}
_id: "+919933221101"
...
SuccessfullConnect: True
...
{TestSignalR_Server.Models.UserProfile}
_id: "+919933221101"
...
SuccessfullConnect: False
...
Saved SomeWork for User : +919933221101
Run Code Online (Sandbox Code Playgroud) 我需要返回有关错误的信息,例如:客户不能有超过 3 个联系人,字段 Job 为空,超出操作限制。
我是否需要使用自己的状态代码发送每个错误?
我可以400 BadRequest用于所有这些错误吗?
需要一些帮助来创建一个通用的方法来按名称选择字段.
这样的事情:
T GetDocField<T>(string doc_Id, string fieldName)
Run Code Online (Sandbox Code Playgroud)
我得到的最好的是使用投影,它给我的文档只有想要的字段:
public T GetDocField<T>(string Doc_Id, string fieldName)
{
var value = DocCollection.Find(d => d.Id == Doc_Id)
.Project<T>(Builders<Doc>.Projection
.Include(new StringFieldDefinition<Doc>
(fieldName))).FirstOrDefaultAsync().Result;
Run Code Online (Sandbox Code Playgroud)
注意: 我正在使用新的c#驱动程序(2.0)
谢谢!!
我需要通过Mongo C#Driver设置分析级别.
Client.GetDatabase("test")返回在运行时IMongoDatabase解析的接口MongoDB.Driver.MongoDatabaseImpl.
根据MongoDB .NET Driver API Documentation MongoDatase类有一个SetProfilingLevel我无法在运行时强制转换的方法.
BTW我还安装了旧版驱动程序版本2.0.1,因为文档中说SetProfilingLevel方法就在其中.