这似乎非常基本,我确信我只是忽略了某个地方的一个班级或一种方法,但对于我的生活,我无法找到它。
我有一个像这样的json字符串:
{ SendId: 4, "Events.Code" : { $all : [2], $nin : [3] } }我可以在mongo shell中针对find()或count()来运行它,并获得我正在寻找的内容。 在C#中处理这个问题的最简单方法是什么? 这是我发现的:
我发现的方法都是想要一个IMongoQuery ,它只是一个标记界面 BsonDocument有一个很好的Parse方法,但它不实现IMongoQuery QueryDocument继承自BsonDocument ,它实现了IMongoQuery ,但它没有它自己的Parse方法,并且我无法将QueryDocument转换为BsonDocument Aggregation框架需要一个BsonDocument [],但有时我只想要一个简单的Find或Count操作 其中一些查询很大且很粗糙,我不想用Query构建器类一次构建它们如果数据库处理json文档,并且我可以在shell中运行这些东西,是不是有某种方法可以通过驱动程序运行它?
This seems so basic that I'm sure I've just overlooked a class or a method somewhere, but for the life of me, I can't find it.
I've got a json string like so:
{ SendId: 4, "Events.Code" : { $all : [2], $nin : [3] } }I can run this in the mongo shell against a find() or a count() and get what I'm looking for. What is the easiest way to deal with this in C#? Here's what I've found:
The methods I'm finding are all wanting an IMongoQuery, which is just a marker interface BsonDocument has a nice Parse method, but it doesn't implement IMongoQuery QueryDocument inherits from BsonDocument, and it does implement IMongoQuery, but it does not have it's own Parse method, and I can't convert the QueryDocument to BsonDocument The Aggregation framework takes a BsonDocument[], but sometimes I just want a simple Find or Count operation Some of these queries are large and gross, and I don't want to build them a line a time with the Query builder classIf the database deals with json documents, and I can run this stuff in the shell, isn't there some way to run it through the driver?
最满意答案
这很丑陋,但你可以通过将字符串反序列化为BsonDocument ,然后将其封装在QueryDocument
BsonDocument query = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>("{ SendId: 4, 'Events.Code' : { $all : [2], $nin : [3] } }"); QueryDocument queryDoc = new QueryDocument(query); var result = collection.FindAs<TypeOfResultExpected>(queryDoc); // or just use Find如果您打算频繁地执行某些操作,则可以将其包装在一个方法中,或者创建一个如下所示的JSQueryDocument类:
public class JSQueryDocument : QueryDocument { public JSQueryDocument(string query) : base(MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(query)) { // Probably better to do this as a method rather than constructor as it // could be hard to debug queries that are not formatted correctly } } /// ... var result = collection.Find(new JSQueryDocument("{ SendId: 4, 'Events.Code' : { $all : [2], $nin : [3] } }"));It's ugly, but you can do this by deserializing the string in to a BsonDocument and then wrapping in a QueryDocument
BsonDocument query = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>("{ SendId: 4, 'Events.Code' : { $all : [2], $nin : [3] } }"); QueryDocument queryDoc = new QueryDocument(query); var result = collection.FindAs<TypeOfResultExpected>(queryDoc); // or just use FindIf it's something you plan on doing frequently, you could always wrap it in a method, or create a JSQueryDocument class like the following:
public class JSQueryDocument : QueryDocument { public JSQueryDocument(string query) : base(MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(query)) { // Probably better to do this as a method rather than constructor as it // could be hard to debug queries that are not formatted correctly } } /// ... var result = collection.Find(new JSQueryDocument("{ SendId: 4, 'Events.Code' : { $all : [2], $nin : [3] } }"));更多推荐
发布评论