简体   繁体   English

MongoDB:在C#驱动程序中构建查询

[英]MongoDB: Build query in C# driver

I stacked to build this Mongodb query in C# driver: 我堆积在C#驱动程序中构建这个Mongodb查询:

{ 
    Location: { "$within": { "$center": [ [1, 1], 5 ] } },
    Properties: {
        $all: [
            { $elemMatch: { Type: 1, Value: "a" } },
            { $elemMatch: { Type: 2, Value: "b" } }
        ]
    }
}

Something next: 下一步:

var geoQuery = Query.WithinCircle("Location", x, y, radius);
var propertiesQuery = **?**;
var query = Query.And(geoQuery, propertiesQuery);

Addition: 加成:

The above query taken from my another question: MongoDB: Match multiple array elements You are welcome to take part in its solution. 上面的查询取自我的另一个问题: MongoDB:匹配多个数组元素欢迎您参与其解决方案。

Here's how if you want to get that exact query: 以下是您希望获得该确切查询的方法:

// create the $elemMatch with Type and Value
// as we're just trying to make an expression here, 
// we'll use $elemMatch as the property name 
var qType1 = Query.EQ("$elemMatch", 
    BsonValue.Create(Query.And(Query.EQ("Type", 1),
                     Query.EQ("Value", "a"))));
// again
var qType2 = Query.EQ("$elemMatch", 
    BsonValue.Create(Query.And(Query.EQ("Type", 2), 
                     Query.EQ("Value", "b"))));
// then, put it all together, with $all connection the two queries 
// for the Properties field
var query = Query.All("Properties", 
    new List<BsonValue> { 
        BsonValue.Create(qType1), 
        BsonValue.Create(qType2)
    });

The sneaky part is that while many of the parameters to the various Query methods expect BsonValue s rather than queries, you can create a BsonValue instance from a Query instance by doing something like: 偷偷摸摸的部分是,尽管各种Query方法的许多参数都需要BsonValue而不是查询,但您可以通过执行以下操作从Query实例创建BsonValue实例:

// very cool/handy that this works
var bv = BsonValue.Create(Query.EQ("Type", 1)); 

The actual query sent matches your original request exactly: 发送的实际查询完全符合您的原始请求:

query = {
  "Properties": {
    "$all": [ 
        { "$elemMatch": { "Type": 1, "Value": "a" }}, 
        { "$elemMatch": { "Type": 2, "Value": "b" }}
    ]
  }
}

(I'd never seen that style of $all usage either, but apparently, it sounds like it's just not documented yet.) (我从来没有见过这种$all使用的风格,但显然,它听起来还没有记录 。)

While I can confirm that the query you posted works on my machine, the documentation of $all seems to indicate that it shouldn't accept expressions or queries , but only values: 虽然我可以确认您发布的查询在我的机器上运行,但$all文档似乎表明它不应该接受表达式或查询 ,而只是接受值:

Syntax: { field: { $all: [ <value> , <value1> ... ] }

(The documentation uses <expression> if queries are allowed, compare to $and ). (如果允许查询,文档使用<expression>$and比较 )。 Accordingly, the C# driver accepts only an array of BsonValue instead of IMongoQuery . 因此,C#驱动程序只接受BsonValue而不是IMongoQuery的数组。

However, the following query should be equivalent: 但是,以下查询应该是等效的:

{
    $and: [
        { "Location": { "$within": { "$center": [ [1, 1], 5 ] } } },
        { "Properties" : { $elemMatch: { "Type": 1, "Value": "a" } } },
        { "Properties" : { $elemMatch: { "Type": 2, "Value": "b" } } }   
    ]
}

Which translates to the C# driver as 这转换为C#驱动程序

var query = 
Query.And(Query.WithinCircle("Location", centerX, centerY, radius),
Query.ElemMatch("Properties", Query.And(Query.EQ("Type", 1), Query.EQ("Value", "a"))),
Query.ElemMatch("Properties", Query.And(Query.EQ("Type", 2), Query.EQ("Value", "b"))));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM