我正在使用MongoDB C# 驱动程序来创建索引
当我的应用程序启动时,它会创建如下索引
await collection.Indexes.CreateOneAsync(new BsonDocument("code", 1), new CreateIndexOptions() { Unique = true, Sparse = true });
Run Code Online (Sandbox Code Playgroud)
我的问题是:如果索引已经存在,则不会再次重新创建/索引索引,对吗?
我正在尝试使用 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) 我有一个模型类,我需要将它保存在 MongoDB 集合中。
我的模型类:
public Class Employee
{
public string EmpID { get; set; }
public string EmpName { get; set; }
public List<Mobile> EmpMobile { get; set; }
}
public Class Mobile
{
public string MobID { get; set; }
public string MobNumber { get; set; }
public bool IsPreferred { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这些值是
Employee EmpInfo = new Employee()
{
EmpID = "100",
EmpName = "John",
EmpMobile = new List<Mobile>()
{
{ MobNumber = "55566610", IsPreferred = …Run Code Online (Sandbox Code Playgroud) MongoDB文档讨论了如何在插入或更新期间触发的集合上定义文档验证器。
例如,可以使用正则表达式验证带有电子邮件字段的集合,插入可能成功或失败,具体取决于创建时集合的配置方式。
我无法为 MongoDB 的 C# 驱动程序找到类似的功能。
这还不支持吗?
我有一本字典,我想用它来更新 mongodb 记录。我正在使用一个简单的 foreach 来迭代字典并构造一个 UpdateDefinition 对象。问题是我无法初始化一个空的 UpdateDefinition 对象,因此我被迫使用现有的键值初始化 UpdateDefinition:
IDictionary<string, object> document = GetDocument();
string firstKey = document.Keys.First();
var update = Builders<BsonDocument>.Update.Set(firstKey, document[firstKey]);
foreach (var key in document.Keys)
{
update = update.Set(key, document[key]);
}
Run Code Online (Sandbox Code Playgroud)
这太可怕了。FilterDefinition 有一个空的 Filter 非常适合此目的。是否有类似的构建迭代 UpdateDefinitions 的方法?
我有以下应用程序:
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, > …
我在 mongodb 中有大量文档集合,只想获取 _id 列表。Mongodb 查询是db.getCollection('Documents').find({},{_id : 0, _id: 1}). 但是在 C# 查询中
IMongoCollection<T> Collection { get; set; }
...
List<BsonDocument> mongoResult = this.Collection.FindAsync(FilterDefinition<T>.Empty, new FindOptions<T, BsonDocument>() { Projection = "{ _id: 0, _id: 1 }" }).Result.ToList();
Run Code Online (Sandbox Code Playgroud)
扔exeptionInvalidOperationException: Duplicate element name '_id'.
我想只有_id列表,不再需要其他的Fileds。文件可能有不同的结构,并排除所有其他手动困难的文件。
什么 C# 查询对应于指定的 mongodb 查询db.getCollection('Documents').find({},{_id : 0, _id: 1}?
更新:不提供与从服务器查询大量数据相关的解决方案,例如
this.Collection.Find(d => true).Project(d => d.Id).ToListAsync().Result;
Run Code Online (Sandbox Code Playgroud) 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; }
我的数据库中有一个集合,用于记录事件。每种类型的事件都有一组不同的数据。我已经用以下类定义了它:
[CollectionName("LogEvent")]
public class LogEvent
{
public LogEvent(string eventType)
{
EventType = eventType;
EventData = new Dictionary<string, object>();
}
public string EventType { get; private set; }
[BsonExtraElements]
public IDictionary<string, object> EventData { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
现在 - 这在某种程度上非常有效。只要EventData字典的元素是简单类型...
var event = new LogEvent("JobQueues"){
EventData = new Dictionary<string, object>(){
{ "JobId": "job-123" },
{ "QueueName": "FastLane" }
}
}
_mongoCollection.InsertOne(event);
Run Code Online (Sandbox Code Playgroud)
...我得到 mongo 文件,如
{
_id: ObjectId(...),
EventType: "JobQueued",
JobId: "job-123",
QueueName: "FastLane"
}
Run Code Online (Sandbox Code Playgroud)
但是一旦我尝试将自定义类型添加到字典中,事情就会停止工作。
var event …Run Code Online (Sandbox Code Playgroud)