我正在尝试对集合进行文本查询并按文本匹配顺序检索结果。文档很好地解释了如何在 shell 中执行此操作:
db.articles.find(
{ status: "A", $text: { $search: "coffee cake" } },
{ score: { $meta: "textScore" } }
).sort( { date: 1, score: { $meta: "textScore" } } )
Run Code Online (Sandbox Code Playgroud)
但它需要将附加score字段从 find投影到排序中。
在 C# 中,我有一个如下所示的函数:
public IEnumerable<T> TextSearch<T>(MongoCollection<T> coll, string text) {
var cursor = coll.Find(Query.Text(text))
.SetSortOrder(SortBy<T>.MetaTextScore(???));
foreach(var t in cursor) {
// strip projected score from value
yield return t;
}
}
Run Code Online (Sandbox Code Playgroud)
但我缺少如何将“textScore”值投影到我的结果中,以便我可以将列指定为MetaTextScorein SetSortOrder。
我需要从“followingList.username”中获取所有用户名并与帖子的用户名进行比较,如果有任何匹配需要将该用户名添加到数组中。
Person Model
{
"_id" : ObjectId("554f20f5c90d3c7ed42303e1"),
"username" : "fatihyildizhan",
"followingList" : [
{
"_id" : ObjectId("55505b6ca515860cbcf7901d"),
"username" : "gumusluk",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58505b6ca515860cbcf7901d"),
"username" : "yalikavak",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58305b6ca515860cbcf7901d"),
"username" : "gumbet",
"avatar" : "avatar.png"
}
]
}
Post Model
{
"_id" : ObjectId("554f2df2a388R4b425b89833"),
"username" : "yalikavak",
"category" : "Summer",
"text" : "blue voyage with yacht"
},
{
"_id" : ObjectId("554f2df2a388P4b425b89833"),
"username" : "yalikavak",
"category" : "Winter",
"text" : "is …Run Code Online (Sandbox Code Playgroud) mongodb mongodb-query mongodb-csharp-2.0 mongodb-.net-driver
如何在 C# 中使用 InsertMany() MongoDB 方法在单个语句中插入多个文档
我的 MongoDB 数据库和连接
IMongoClient _client;
IMongoDatabase _database;
_client = new MongoClient();
_database = _client.GetDatabase("test");
var collection = _database.GetCollection<BsonDocument>("EmpInfo");
Run Code Online (Sandbox Code Playgroud)
我有一个收藏 - BsonArray
var EmpInfoArray = new BsonArray {
new BsonDocument
{
{"EmpID", "100"},
{"EmpName", "John"},
{"EmpMobile", new BsonArray
{
new BsonDocument {
{"MobNumber", "55566610"},
{"IsPreferred", true},
{"IsLive", false}
},
new BsonDocument {
{"MobNumber", "55566611"},
{"IsPreferred", true},
{"IsLive", true}
},
}
},
{"IsLive", true}
},
new BsonDocument
{
{"EmpID", "101"},
{"EmpName", "Peter"},
{"EmpMobile", new BsonArray
{ …Run Code Online (Sandbox Code Playgroud) 我有一个类属性名称作为字符串变量,并希望在 LINQ 查询中使用它。下面的例子:
public class Demo
{
public string prop1 {get; set;}
public string prop2 {get; set;}
public string prop3 {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我可以做这个
var data = db.Single<Demo>(d => d.prop1 == "value");
Run Code Online (Sandbox Code Playgroud)
但不知道运行时的属性是什么并获取该字符串参数,例如
string propname = "prop2";
Run Code Online (Sandbox Code Playgroud)
有没有可能在 lambda 表达式中使用它d => d.propname == "value"?我不确定它可以,而且在逻辑上似乎不可能。所以想到发布一个问题,看看是否有办法。请建议。
请注意,该Single()调用正在发生MongoDB C# Driver,因此不确定反射是否有效。
我想使用 Mongo 驱动程序测试我对 MongoWriteException 的处理,这是一个示例方法:
private void Update()
{
try
{
var find = Builders<Filter>.Filter.Eq(e => e.Id, "someId");
var update = Builders<Filter>.Update.Set(e => e.SomeValue, "AValue");
_documentStore.MongoCollection<Filter>().UpdateOne(find, update, new UpdateOptions { IsUpsert = true }, CancellationToken.None);
}
catch (MongoWriteException mongoWriteException)
{
if (mongoWriteException.WriteError.Category != ServerErrorCategory.DuplicateKey)
{
throw;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我如何模拟 MongoWriteException?我试图像这样构建它:
var mongoWriteException = new MongoWriteException(new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("d", 2)), 0), new WriteError(), // <- Protected constructor
Run Code Online (Sandbox Code Playgroud)
但是 WriteError 类有一个内部构造函数
我使用c#mongodb驱动程序。当我想更新我的特定值时,它将引发异常。我以前用过,但现在不知道如何使用,但是以前没有任何错误。这是我的代码:
var result = await col.UpdateManyAsync(
p => p.X > 5,
Builders<Payment>.Filter.Gt(p => p.Amount, 100).Set("Level", "High")
);
Run Code Online (Sandbox Code Playgroud)
这是我的付款课程:
public class Payment
{
public ObjectId Id { get; set; }
public decimal Amount { get; set; }
public Type Type { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 使用最新的 C# mongodb 驱动程序和 .NET 4.5.1。
我想在玩家之间进行一些定制的比赛。假设我有以下模型。
public sealed class PlayerPoints
{
[BsonId]
public ObjectId PlayerId;
public DateTime CreateDate;
public int Points;
public int[] SeasonalPoints;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够在特定SeasonalPoints索引之间获得玩家的排名。
一个例子:
{PlayerId : someId1, CreateDate : <someCreateDate>, Points : 1000, SeasonalPoints : [100,100,100,100,100,100,100,100,100,100,100]}
{PlayerId : someId2, CreateDate : <someCreateDate>, Points : 1000, SeasonalPoints : [100,100,100,100,100,100,100,100,50,150,100]}
{PlayerId : someId3, CreateDate : <someCreateDate>, Points : 1100, SeasonalPoints : [200,100,100,100,100,100,100,100,0,0,300]}
Run Code Online (Sandbox Code Playgroud)
请注意,这里有 10 个季节。我正在搜索一个查询,该查询根据玩家的排名返回排序的玩家列表。排名由提供的索引之间的点的总和设置。
如果我查询第 9 季到第 10 季的排名,那么 someId3 是第一个,someId2 之后,someId1 是最后一个。如果我在第 7-9 季查询排名,那么 …
我有一个 A 班和一个 B 班和 C 班
public class A
{
public string Id {get;set;}
public List<B> Children {get;set;}
}
public class B
{
public string Id {get;set;}
public string Foo {get;set;}
public double Bar {get;set;}
}
public class C
{
public string Id {get;set;}
//This property will hold a serialized version of Class A.
//The scenario requirement is that it can hold any arbitrary BsonDocument of different types
public BsonDocument Properties {get;set;}
}
var instanceOfClassC = collection.Find(...).First();
var data …Run Code Online (Sandbox Code Playgroud) 我们的项目中有一个webAPI,它非常简单,但它在执行后不会释放内存,每次执行时都会增加使用的内存。
我使用 webAPI 2.0 和 MongoDB 作为后端服务器。
public class LogsController:ApiController
{
BsonDocument __docs;
IMongoDatabase __db;
IMongoCollection<BsonDocument> __docsColl;
[Route("api/Logs")]
public async Task<int> Post(RequestData logs)
{
if (logs.Token == "I")
{
__db = new MongoClient(new MongoClientSettings
{
Server = new MongoServerAddress("serverIP", 27017),
Credentials = new[] { MongoCredential.CreateCredential("database", "user name", "password") }
}).GetDatabase("connect_database");
__docs = new BsonDocument()
{
{ "Customer",logs.Customer}
};
__docsColl = __db.GetCollection<BsonDocument>("InsertData");
await __docsColl.InsertOneAsync(__docs);
}
logs = null;
return 1;
}
protected override void Dispose(bool disposing)
{
__docs = null;
__db …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc-4 asp.net-web-api asp.net-web-api2 mongodb-.net-driver
我用 C# 学习 mongodb,我想创建一个包含实体列表的记录,但子实体没有生成他的对象 ID。
这是我的代码:
public class Curso
{
[BsonId]
public ObjectId _id { get; set; }
[BsonElement("descripcion")]
public string descripcion { get; set; }
[BsonElement("horas")]
public int horas { get; set; }
[BsonElement("alumnos")]
public IEnumerable<Alumno> alumnos { get; set; }
public Curso()
{
}
}
Run Code Online (Sandbox Code Playgroud)
校友实体:
public class Alumno
{
[BsonId]
public ObjectId _id { get; set; }
[BsonElement("name")]
public string name { get; set; }
[BsonElement("age")]
public int age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我插入了一个新课程,自动生成 id Curso …