我有一个名为"服务器"的集合,包含以下文档.
{
name: "West",
ip: "123.123.123.123",
channels:
[
{
name: "English",
port: "1234",
status: "0"
},
{
name: "Spanish",
port: "1235",
status: "0"
},
{
name: "German",
port: "1236",
status: "0"
}
]
},
{
name: "East",
ip: "122.122.122.122",
channels:
[
{
name: "English",
port: "1234",
status: "0"
},
{
name: "French",
port: "1235",
status: "0"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我如何使用C#using结构从MongoDB中选择它?
让我们说我们有一个关键值,它们的意义上是多态的.考虑下一个示例项目:
public class ToBeSerialized
{
[BsonId]
public ObjectId MongoId;
public IDictionary<string, BaseType> Dictionary;
}
public abstract class BaseType
{
}
public class Type1 : BaseType
{
public string Value1;
}
public class Type2:BaseType
{
public string Value1;
public string Value2;
}
internal class Program
{
public static void Main()
{
var objectToSave = new ToBeSerialized
{
MongoId = ObjectId.GenerateNewId(),
Dictionary = new Dictionary<string, BaseType>
{
{"OdEd1", new Type1 {Value1="value1"}},
{
"OdEd2",
new Type1 {Value1="value1"}
}
}
};
string connectionString = …Run Code Online (Sandbox Code Playgroud) 认为这将是非常直接的,但我的价值保持不变(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) 随着mongodb的10gen c#驱动程序的最新更新,我想更新我的代码,因此它使用强类型版本.
我之前的电话是:
var update2 = new UpdateBuilder();
var index = album.Ratings.IndexOf(rating);
update2.Set("Ratings." + index + ".Number", number);
update2.Set("Rating", album.Rating);
_session.Db().GetCollection<Album>("Album")
.Update(Query<Album>.Where(x => x.Id == objId), update2); //this line is working
Run Code Online (Sandbox Code Playgroud)
新的电话会是:
update.Set(x => x.Ratings[index].Number, number);
//update2.Set("Ratings." + index + ".Number", number); previous call
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个例外:
无法确定表达式的序列化信息:(Album x)=> x.Ratings.get_Item(WebApp.Areas.API.Controllers.RatingController + <> c__DisplayClass5.index).Number.
有什么方法可以更新List中的项目吗?
我正在使用Mongodb作为我的asp.net mvc前端站点的数据库.我在3台服务器上运行MongoDB,在副本集中运行,主服务器,辅助服务器和仲裁服务器.连接到这是3个前端Web服务器,它们对Mongo中保存的数据执行CRUD操作.我对我的设置有很多疑问,我想澄清一下.
这是我来自C#的连接字符串
server=myprimary.com:27017,mysecondary.com:27017;replicaset=MySet;safe=true;database=MyDatabase
Run Code Online (Sandbox Code Playgroud)
在此连接字符串中包含仲裁者是否正确?
当我使用sql server时,我使用集成安全性设置所有连接字符串.Mongo连接字符串中类似的最佳做法是什么?
我一直在阅读MongoDB中的Write Concern.我知道有几个级别决定了写操作成功的保证级别,并且设置此级别的性能越高,性能权衡越大.但是,我正在C#环境中工作,我正在尝试弄清楚如何在那里使用Write Concern以及哪些级别对某些情况最有效.我已经想出如何使用WriteConcernResult对象收集检查结果,我主要对关卡本身感兴趣.
这些是我的问题:
如何在C#中为特定写入设置写入关注级别?
这个答案建议使用连接字符串,但这看起来像一个全局设置,我不想要,因为我将使用的一些写操作比其他更"重要",我不想杀死性能.我注意到有一个WriteConcern类,但文档中没有详细介绍它的使用(它在文档中的MongoDB.Driver命名空间下).
特别是,如何将其设置为"Journaled"或"Replica Acknowledged",默认情况下它是"已确认"?
对于每个级别,哪些类型的问题可以通过Write Concern检查?
例如:系统崩溃,电源故障,网络连接问题,等我的东西通过偷渡特别感兴趣,是不容易发现,因为停电等等是非常明显的,我们可以估算,其中操作可能已失败的时间间隔并做出相应的反应
我正在尝试使用 C# 在 MongoDB 中的嵌套数组文档中实现文本搜索功能。我有以下格式的 MongoDB 集合。
{
"_id" : ObjectId("56c6f03ffd07dc1de805e84f"),
"Customers" : {
"Contact" : [
[
{
"FirstName" : "Swetha",
"LastName" : "DevAnand"
}
]
]
}
}
Run Code Online (Sandbox Code Playgroud)
蒙戈查询:
db.test_collection.find({"Customers.Contact":{$elemMatch:{$elemMatch:{LastName: /.*Dev.*/}}}});
Run Code Online (Sandbox Code Playgroud)
在上面的 MongoDB 查询中,我想使用 C# 对 BSON 文档的最内层数组进行 LIKE 搜索。但是,我已经在 Mongo Query 中使用嵌套的 $elemMatch 实现了这一点。但是在 C# 中尝试做同样的事情时,它需要每个 ElemMatch 查询的字段名。在上面的例子中,最里面的 Contact 字段数组没有任何明确的字段名。到目前为止,我已经尝试了以下所有 C# 代码来实现该场景,但它们都没有帮助。
var regex = new BsonRegularExpression(searchVal,"i");
query = Query.And(Query.ElemMatch("Customers.Contact", Query.And(Query.ElemMatch("LastName", Query.Matches("LastName", regex)))));
query = Query.And(Query.Matches("Customers.Contact.$.LastName",regex));
query = Query.And(Query.ElemMatch("Customers.Contact.$", Query.EQ("LastName", regex)));
query = Query.And(Query.ElemMatch("Customers.Contact.$", Query.Matches("LastName", regex)));
query …Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用 Linq 将表达式的动态列表传递给 MongoDB C# 驱动程序查询......例如,此方法适用于针对 ORM 的常规 Linq 查询,但在应用于 MongoDB 查询时会导致错误...(仅供参考:我也在使用 LinqKit 的 PredicateBuilder)
//
// I create a List of Expressions which I can then add individual predicates to on an
// "as-needed" basis.
var filters = new List<Expression<Func<Session, Boolean>>>();
//
// If the Region DropDownList returns a value then add an expression to match it.
// (the WebFormsService is a home built service for extracting data from the various
// WebForms Server Controls... in case you're wondering …Run Code Online (Sandbox Code Playgroud) 我有以下应用程序:
A - 应用程序 A 是托管在 IIS 7.5 中的 .net wcf 服务,使用在 .net 4.5 中编译的 c# mongodriver 2.2.4
B - 应用程序 B 是使用 mongodriver 1.11 在 .net 3.5 中编译的 Windows 服务应用程序
这两种服务是相似的,服务 B 是为遗留系统维护的,服务 A 正在演变。
这两个应用程序都托管在相同的服务器中。(Windows Standard 2008 R2) 这个应用程序已经完美运行了 1 年多,但自 2016 年 6 月 24 日起,应用程序 A (WCF) 在打开与 Mongo Server 的新连接时开始出现奇怪的行为:
Run Code Online (Sandbox Code Playgroud)> System.TimeoutException: A timeout occured after 30000ms selecting a > server using CompositeServerSelector{ Selectors = > ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, > …
Using MongoDB C# Driver 2.3, what's the strongly typed version of the following code?
database.GetCollection<GamePlay>()
.Indexes
.CreateOne("{ \"PartiesInGame.Username\": 1 }");
Run Code Online (Sandbox Code Playgroud)
GamePlay有一个IEnumerable<PartyInGame>,其中PartyInGame有一个public string Username { get; set; }