用php实现mongodb聚合查询
我有数据库名为travel和collection名为sample的mongodb,数据如下用php实现mongodb聚合查询,php,mongodb,aggregation-framework,Php,Mongodb,Aggregation Framework,我有数据库名为travel和collection名为sample的mongodb,数据如下 `{ "_id": { id": "2344" }, "places": { "destination": [ "singapore", "delhi" ] }, "dt": { "date": "1434467400" } }, { "_id": { id": "2345" }, "places": { "destination": [ "singapore",
`{
"_id": { id": "2344" },
"places": { "destination": [ "singapore", "delhi" ] },
"dt": { "date": "1434467400" }
},
{
"_id": { id": "2345" },
"places": { "destination": [ "singapore", "delhi" ] },
"dt": { "date": "1434467445" }
},
{
"_id": { "id": "2354" },
"places": { "code": "7856", "source": [ "beijing", "singapore" ], "destination": [ "newyork", "landon", "sidney" ] },
"dt": { "date": "1434589338" }
}`
我编写了monogdb聚合函数来查找位置数
`db.sample.aggregate([{$group : {_id : "$places", count : {$sum : 1}}}, {$sort: {count: 1}}])`
结果显示为
{ "_id" : { "destination" : [ "singapore", "delhi" ] }, "count" : 2 } \
{ "_id" : { "code" : "7856", "source" : [ "beijing", "singapore" ], "destination
" : [ "newyork", "landon", "sidney" ] }, "count" : 1 }
它通过对位置进行分组来计算每个位置的数量
我有以下两个要求
thanx提前获得帮助。你好,Surendra,欢迎使用Stack Overflow。很抱歉不得不这么说,但是你的问题并不适合这个网站。如果您根本不知道如何编写查询mongodb的php,那么您需要找到合适的教程或课程。如果您正在编写代码,并且遇到了需要帮助的特定问题,并且可以显示无法正常工作的代码,那么您可以返回堆栈溢出。请看一看以获得一些指导。嗨,VinceBowdren,谢谢你的建议。我会尽我最大的努力回答适当的问题。