在 C# 中的 mongodb aggregate() 函数驱动程序的管道中使用 $and
Using $and in the pipeline for mongodb aggregate() function driver in C#
我正在尝试使用 C# 中的 mongodb 聚合框架。
我希望能够将 $and 放入我的查询中。
这是我想要 运行-
的 mongodb 查询
db.students.aggregate(
{
$match:
{
name:"mira",
$and:[{date:{$gte:ISODate("2015-03-01T00:00:00")}},
{date:{$lte:ISODate("2015-04-01T00:00:00")}}]
},
{
$group:{"_id":"$subject", "$sum":"$marks"}
}
)
我创建了对应于 mongodb 查询的匹配项,但这里有些地方不对,因为我在括号中遇到了编译器错误。
我的 C# 代码如下-
var match = new BsonDocument {
{
"$match",
new BsonDocument
{
{
"name", "mira"
}
},
{
"$and", new BsonDocument{ {
new BsonDocument
{
{ "date",
new BsonDocument { { "$gte", beginDate.ToString("yyyy-MM-ddThh:mm:ss") } }
}
},
new BsonDocument
{
{ "date",
new BsonDocument { { "$lte", endDate.ToString("yyyy-MM-ddThh:mm:ss") } }
}
}
}}
}
} } ;
有人可以指导我如何将 $and in 匹配到我的 C# 代码中吗?
不需要在你的$match
查询中使用$and
,这可以重新写作:
var start = ISODate("2015-03-01T00:00:00"),
end = ISODate("2015-04-01T00:00:00");
db.students.aggregate(
{
"$match": {
"name": "mira",
"date": { "$gte": start, "$lte": end}
}
},
{
"$group": {"_id":"$marks", "$sum":"$marks"}
}
);
然后 $match
管道可以用 C# 编写为
var match = new BsonDocument
{
{
"$match",
new BsonDocument
{
{ "name", "mira" },
{ "date", new BsonDocument
{
{"$gte", beginDate.ToString("yyyy-MM-ddThh:mm:ss")},
{"$lte", endDate.ToString("yyyy-MM-ddThh:mm:ss") }
}
}
}
}
};
你可以这样做:
var match= new BsonDocument("$match", Query.And(Query.EQ("name", "mira"),
Query.GTE("date", beginDate.ToString("yyyy-MM-ddThh:mm:ss")),
Query.LTE("date", endDate.ToString("yyyy-MM-ddThh:mm:ss"))).ToBsonDocument());
或者您也可以遵循@chridam 推荐的变体:
var match = new BsonDocument
{
{
"$match",
new BsonDocument
{
{ "name", "mira" },
{ "date", new BsonDocument
{
{"$gte", beginDate.ToString("yyyy-MM-ddThh:mm:ss")},
{"$lte", endDate.ToString("yyyy-MM-ddThh:mm:ss") }
}
}
}
}
};
我正在尝试使用 C# 中的 mongodb 聚合框架。
我希望能够将 $and 放入我的查询中。 这是我想要 运行-
的 mongodb 查询db.students.aggregate(
{
$match:
{
name:"mira",
$and:[{date:{$gte:ISODate("2015-03-01T00:00:00")}},
{date:{$lte:ISODate("2015-04-01T00:00:00")}}]
},
{
$group:{"_id":"$subject", "$sum":"$marks"}
}
)
我创建了对应于 mongodb 查询的匹配项,但这里有些地方不对,因为我在括号中遇到了编译器错误。 我的 C# 代码如下-
var match = new BsonDocument {
{
"$match",
new BsonDocument
{
{
"name", "mira"
}
},
{
"$and", new BsonDocument{ {
new BsonDocument
{
{ "date",
new BsonDocument { { "$gte", beginDate.ToString("yyyy-MM-ddThh:mm:ss") } }
}
},
new BsonDocument
{
{ "date",
new BsonDocument { { "$lte", endDate.ToString("yyyy-MM-ddThh:mm:ss") } }
}
}
}}
}
} } ;
有人可以指导我如何将 $and in 匹配到我的 C# 代码中吗?
不需要在你的$match
查询中使用$and
,这可以重新写作:
var start = ISODate("2015-03-01T00:00:00"),
end = ISODate("2015-04-01T00:00:00");
db.students.aggregate(
{
"$match": {
"name": "mira",
"date": { "$gte": start, "$lte": end}
}
},
{
"$group": {"_id":"$marks", "$sum":"$marks"}
}
);
然后 $match
管道可以用 C# 编写为
var match = new BsonDocument
{
{
"$match",
new BsonDocument
{
{ "name", "mira" },
{ "date", new BsonDocument
{
{"$gte", beginDate.ToString("yyyy-MM-ddThh:mm:ss")},
{"$lte", endDate.ToString("yyyy-MM-ddThh:mm:ss") }
}
}
}
}
};
你可以这样做:
var match= new BsonDocument("$match", Query.And(Query.EQ("name", "mira"),
Query.GTE("date", beginDate.ToString("yyyy-MM-ddThh:mm:ss")),
Query.LTE("date", endDate.ToString("yyyy-MM-ddThh:mm:ss"))).ToBsonDocument());
或者您也可以遵循@chridam 推荐的变体:
var match = new BsonDocument
{
{
"$match",
new BsonDocument
{
{ "name", "mira" },
{ "date", new BsonDocument
{
{"$gte", beginDate.ToString("yyyy-MM-ddThh:mm:ss")},
{"$lte", endDate.ToString("yyyy-MM-ddThh:mm:ss") }
}
}
}
}
};