服务器环境:mongo server version 3.0.10
内容格式:
{
    "_id" : ObjectId("582bc1b1421e46e4c14e2be8"),
    "date" : "2016-10-21",
    "path" : [
        {
            "url" : "http://m.xin.com/quanguo/sanling/yishen/?q=三菱翼神",
            "query" : "三菱翼神",
            "dttime" : "2016-10-21 12:41:36",
            "time_stamp" : 1477024896,
            "platform" : "m"
        },
        {
            "url" : "http://m.xin.com/quanguo/sanling/yishen/?q=三菱翼神",
            "query" : "三菱翼神",
            "dttime" : "2016-10-21 12:41:36",
            "time_stamp" : 1477024896,
            "platform" : "m"
        }
    ],
    "cid" : "ecabb21f-cb89-992f-79d9-b920427097bf"
}
查询语句:
db.dw_all_query_user_path.find({"path.query":/三菱/i},{"path":{"$elemMatch":{"query": /三菱/i}}})
经测试$elemMatch只能返回数组中的一个文档。
请问如何能将path数组中的数据全部返回?
使用聚合
db.dw_all_query_user_path.aggregate(
{
    $match:{
        "path.query":/奔驰/i
    }
},
{
    $unwind:'$path'
},
{
    $match:{
        'path.query':/奔驰/i
    }
},
{
    $group:{
        _id:'$_id',
        cid:{$first:'$cid'},
        date:{$first:'$date'},
        path:{$push:'$path'}
    }
}).pretty()
最终还是得到了需要的数据格式。感谢!!!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
使用aggregate试试呢
http://stackoverflow.com/a/15...