英文:
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<YourClass>()
.Where(x => x.Age == 65 || x.Age == "65")
.ToList();
Using $in
(.In()
)
var result = collection
.AsQueryable<YourClass>()
.Where(x => x.age.In(new[] { 65, "65" }))
.ToList();
Or something like this (according to this question)
Using $or
:
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Eq("age", 65) | builder.Eq("age", "65");
db.collection.Find(filter).ToList();
Using $in
:
var builder = Builders<BsonDocument>.Filter;
var filter = builder.In("age", new object[] { 65, "65" });
db.collection.Find(filter).ToList();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论