认为这将是非常直接的,但我的价值保持不变(0).
我想做的是当用户收到他们没有读过的消息时递增我的UnreadMessages字段,然后在他们有时删除它.所以我认为像这样的代码会起作用:
var userHelper = new MongoHelper<User>();
//increment
userHelper.Collection.Update(Query.EQ("Id", userId.ToId()), Update.Inc("UnreadMessages", 1));
//decrement
userHelper.Collection.Update(Query.EQ("Id", userId.ToId()), Update.Inc("UnreadMessages", -1));
Run Code Online (Sandbox Code Playgroud)
运行后,不会抛出任何错误,但值也不会改变.不,我不是一个接一个地运行,因为上面的代码可以被解释:)
更新
这是我的帮助类:
public class MongoHelper<T> : Sandbox.Services.IMongoHelper<T> where T : class
{
public MongoCollection<T> Collection { get; private set; }
public MongoHelper()
{
var con = new MongoConnectionStringBuilder(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString);
var server = MongoServer.Create(con);
var db = server.GetDatabase(con.DatabaseName);
Collection = db.GetCollection<T>(typeof(T).Name.ToLower());
}
}
Run Code Online (Sandbox Code Playgroud)
感谢特拉维斯的回答我能够解决这个问题:
MongoHelper<UserDocument> userHelper = new MongoHelper<UserDocument>();
var user = userHelper.Collection.FindAndModify(Query.EQ("Username", "a"), SortBy.Null, Update.Inc("MessageCount", 1), true).GetModifiedDocumentAs<UserDocument>();
Run Code Online (Sandbox Code Playgroud) 我在10gen Mongo C#驱动程序的v2中使用了新的Fluent Aggregation Pipeline,但在尝试按多个字段分组时遇到异常(下面的示例代码).
抛出的异常是......
命令聚合失败:异常:必须将组聚合字段"Month"定义为对象内的表达式.
我可以通过为我的组密钥创建一个类型来使用它,但是我更喜欢使用匿名类型,因为我需要创建的类型不会用于其他目的.
var agg = db.GetCollection<Order>("orders").Aggregate();
var project = agg.Project(o => new {o.Value
, o.Product
, Month = o.Date.Month
, Year = o.Date.Year});
var group = project.Group(
key => new { key.Month, key.Product},
g => new OrderSummary {Month = g.Key.Month
,Product = g.Key.Product
, TotalSales = g.Sum(o => o.Value)});
var result = group.ToListAsync().Result;
Run Code Online (Sandbox Code Playgroud)
以供参考 ...
public class Order : Entity
{
public DateTime Date { get; set; }
public string Product { get; set; …Run Code Online (Sandbox Code Playgroud) c# mongodb 10gen-csharp-driver aggregation-framework mongodb-.net-driver
我有以下结构:
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
我试图通过ASP.NET MVC 4中内置的REST api退出MongoDB集合中的所有文档,当我输入localhost时遇到错误:50491/api/document:
反序列化类Acord_Rest_API.Models.Document的Id属性时发生错误:无法从BsonType ObjectId反序列化字符串.
我的控制器看起来像:
public class DocumentController : ApiController
{
public readonly MongoConnectionHelper<Document> docs;
public DocumentController()
{
docs = new MongoConnectionHelper<Document>();
}
public IList<Document> getAllDocs()
{
var alldocs = docs.collection.FindAll();
return alldocs.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
我的MongoDB文档是这样的吗?

我在这里错过了什么?
我连接到DB的类:
public class MongoConnectionHelper<T> where T: class
{
public MongoCollection<T> collection { get; private set; }
public MongoConnectionHelper()
{
string connectionString = "mongodb://127.0.0.1";
var server = MongoServer.Create(connectionString);
if (server.State == MongoServerState.Disconnected)
{
server.Connect();
}
var conn = server.GetDatabase("Acord");
collection = conn.GetCollection<T>("Mappings"); …Run Code Online (Sandbox Code Playgroud) 我使用c#和asp.net mvc(visual studio 2015).当我尝试将mongodb连接到c#时,会出现此错误:
MongoDB.Driver.MongoConfigurationException: The connection string 'mongodb:://localhost' is not valid.
Run Code Online (Sandbox Code Playgroud)
和错误来源是:
var client = new MongoClient(Settings.Default.bigdataconexionstrin);
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WebApplication5.Properties;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
namespace WebApplication5.Controllers
{
[Authorize]
public class HomeController : Controller
{
public IMongoDatabase db1;
public HomeController()
{
var client = new MongoClient(Settings.Default.bigdataconexionstrin);
MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress("localhost", 27017);
this.db1 = client.GetDatabase(Settings.Default.db);
}
public ActionResult Index()
{
return View();
}
}
}
Run Code Online (Sandbox Code Playgroud)