MongoDB C#客户端在使用相同操作符的$or查询时出现问题。

huangapple go评论66阅读模式
英文:

mongodb c# client problem with $or query with the same operator

问题

MongoDB Compass中,我可以像以下这样查询一个集合:
`{ age: { $eq: "65", $eq: 65} }`

正如你可能会问的那样 - 我不知道age是字符串还是数字
C#中,我该如何进行这样的查询?
我尝试过使用`IEnumerable<KeyValuePair<string, object>>` 并将其传递给`BsonDocument` - 但是我得到了一个重复键的错误,这是显而易见的,因为有两个$eq操作符
但在C#中要如何做才对呢?

var para = comparisonOperator.Parameter.Name;
var op = FilterComparisonOperators.GetComparisonMongoDbOperator(comparisonOperator.Name);
var value = comparisonOperator.Value.Name;
if (int.TryParse(value, out int intValue))
{
    var bsonDocument = new BsonDocument(op, intValue);
    filterDocument.AddRange(new BsonDocument(para, bsonDocument));
    filterDocument.AddRange(new BsonDocument(para, bsonDocument));
}
else
{
    var bsonDocument = new BsonDocument(op, value);
    filterDocument.AddRange(new BsonDocument(para, bsonDocument));
}

我想查询一个像65这样的数字,无论是作为数字还是字符串。
英文:

In MongoDb Compass I can query a collection like the following:
{ age: { $eq: "65", $eq: 65} }

As you might ask - I don't know if age is a string or number.
How can I make such a query in c#?
I tried a IEnumerable<KeyValuePair<string, object>> and give this to a BsonDocument - but I get an error with duplicate keys which is obvious cause there are two $eq operators.
But how to do it right in c#?

var para = comparisonOperator.Parameter.Name;
var op = FilterComparisonOperators.GetComparisonMongoDbOperator(comparisonOperator.Name);
var value = comparisonOperator.Value.Name;
if (int.TryParse(value, out int intValue))
{
    var bsonDocument = new BsonDocument(op, intValue);
    filterDocument.AddRange(new BsonDocument(para, bsonDocument));
    filterDocument.AddRange(new BsonDocument(para, bsonDocument));
}
else
{
    var bsonDocument = new BsonDocument(op, value);
    filterDocument.AddRange(new BsonDocument(para, bsonDocument));
}

I want to query a number like 65 as number or string.

答案1

得分: 0

你可以像这个查询一样使用$or(链接在这里)或者像这个查询一样使用$in(链接在这里),这样你可以尝试类似Linq方式的操作:

使用$or||

var result = collection
               .AsQueryable<YourClass>()
               .Where(x => x.Age == 65 || x.Age == "65")
               .ToList();

使用$in.In()

var result = collection
               .AsQueryable<YourClass>()
               .Where(x => x.age.In(new[] { 65, "65" }))
               .ToList();

或者类似这样的方式(根据这个问题):

使用$or

var builder = Builders<BsonDocument>.Filter;
var filter = builder.Eq("age", 65) | builder.Eq("age", "65");
db.collection.Find(filter).ToList();

使用$in

var builder = Builders<BsonDocument>.Filter;
var filter = builder.In("age", new object[] { 65, "65" });
db.collection.Find(filter).ToList();
英文:

You can use $or like this query or $in like this other query so you can try something like this in Linq way:

Using $or (||)

var result = collection
               .AsQueryable&lt;YourClass&gt;()
               .Where(x =&gt; x.Age == 65 || x.Age == &quot;65&quot;)
               .ToList();

Using $in (.In())

var result = collection
               .AsQueryable&lt;YourClass&gt;()
               .Where(x =&gt; x.age.In(new[] { 65, &quot;65&quot; }))
               .ToList();

Or something like this (according to this question)

Using $or:

var builder = Builders&lt;BsonDocument&gt;.Filter;
var filter = builder.Eq(&quot;age&quot;, 65) | builder.Eq(&quot;age&quot;, &quot;65&quot;);
db.collection.Find(filter).ToList();

Using $in:

var builder = Builders&lt;BsonDocument&gt;.Filter;
var filter = builder.In(&quot;age&quot;, new object[] { 65, &quot;65&quot; });
db.collection.Find(filter).ToList();

huangapple
  • 本文由 发表于 2023年2月8日 23:03:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定