下面是我想使用mongo-php-library运行的查询,我想将查询作为字符串传递给驱动程序。不想将 json 转换为 php 数组。
db.getCollection('_survey.response').aggregate([
{
$match:
{
$and:[
{
"Provider.Name": {
$in:[
"Comcast",
"AT&T"
]
}
},
{
CreatedOn: {
$lt: ISODate('2017-05-10'),
$gt: ISODate('2017-01-01')
}
}
]
}
},
{
$group:{
_id: {
Status : "$Status",
Survey: "$SurveyName"
},
SurveyId: {$addToSet: "$SurveyId"},
Count: {$sum: 1}
}
},
{
$group:{
_id: {
SurveyName: "$_id.Survey"
},
Status: {$push: "$_id.Status"},
Count : { $push : "$Count" }
}
},
{
$project:{
_id: 0,
SurveyId: "$_id.SurveyId",
Survey: "$_id.SurveyName",
Status: 1,
Count: 1
}
}
]);
这是我正在使用的库,mongo-php
我已经看到了这个问题。
我已经使用 mongo c# 驱动程序做了类似的事情。下面是 c# 代码,
static void TestMongoShellV3()
{
var start = new DateTime(2017, 01, 01, 9, 57, 44);
var end = new DateTime(2017, 12, 01, 18, 5, 18);
string dbname = "SURVEY_POC_MOIZ";
string collname = "_survey.response";
_database = _client.GetDatabase(dbname);
var collection = _database.GetCollection<BsonDocument>(collname);
string bson = @"[
{
$group:{
_id: {
Status : '$Status',
Survey: '$SurveyName',
SurveyId: '$SurveyId'
},
Count: {$sum: 1}
}
},
{
$group:{
_id: {
SurveyName: '$_id.Survey',
SurveyId: '$_id.SurveyId',
},
Status: {$push: '$_id.Status'},
Count : { $push : '$Count' }
}
},
{
$project:{
_id: 0,
SurveyId: '$_id.SurveyId',
Survey: '$_id.SurveyName',
Status: 1,
Count: 1
}
},
{
$sort: {Survey: -1}
}
];";
var pipeline = BsonSerializer.Deserialize<BsonArray>(bson).Select(p => p.AsBsonDocument).ToList<BsonDocument>();
var result = collection.Aggregate<BsonDocument>(pipeline).ToList();
showResult(result);
}
您无法使用新驱动程序执行此操作,因为它不再具有执行方法。以下是文档中的附加内容:
警告
此方法调用的 » eval 命令在 MongoDB 3.0+ 中已弃用。
关于这一点:
不想将 json 转换为 php 数组。
这是唯一的解决方案。在执行查询之前,可以使用json_decode
以编程方式将string
转换为array
。