没有字段名称的Mongoose结构聚合输出

时间:2015-03-16 12:09:30

标签: javascript node.js mongodb mongoose aggregation-framework

我正在使用聚合来从子文档中分页数据。子文档没有严格的模式,因此每个文档可能不同,这使我很难构造输出,因为我不知道子文档的字段名称。

我在使用什么

Mongo 3.0.0

节点0.10.33

猫鼬3.9.7

我的模特

var BodySchema = new Schema({random: String},{strict:false});

var FeedSchema = new Schema({
    name: String,
    body:[BodySchema]
});

我的数据看起来像这样

[{
    _id:"...",
    name:"Power Rangers feed",
    body:[
        {
            "_id":"...",
            "name" : "Jason",
            "colour" : "Red",
            "animal" : "T-rex"
        },
        {
            "_id":"...",
            "name" : "Billy",
            "colour" : "Blue",
            "animal" : "Triceratops"
        },
        {
            "_id":"...",
            "name" : "Zach",
            "colour" : "Black",
            "animal" : "Mastadon"
        }
    ]
},
{
    _id:"...",
    name:"Transformers feed",
    body:[
        {
            "_id":"...",
            "name" : "Optimus Prime",
            "team" : "Autobots",
            "class" : "leader"
            "alt-mode" : "truck"
        },
        {
            "_id":"...",
            "name" : "Bumblebee",
            "team" : "Autobots",
            "class" : "scout"
            "alt-mode" : "VW Beetle"
        },
        {
            "_id":"...",
            "name" : "Blaster",
            "team" : "Autobots",
            "class" : "Commmunicator"
            "alt-mode" : "Sterio"
        },
        {
            "_id":"...",
            "name" : "Hotrod",
            "team" : "Autobots",
            "class" : "Warrior"
            "alt-mode" : "Car"
        }
    ]
}]

我当前的汇总代码如下所示

feed.aggregate([
    {'$match':{_id:id('550234d3d06039d507d238d8')}},
    {'$unwind':'$body'},
    {'$skip':2},
    {'$limit':2},
    {
        '$project': {
            '_id':"$body._id",
            'body': "$body"
        }

    },
], function(err, result){

    if(err){return(res.send(500, err))}

    res.send(result);

});

我当前的结果如下所示

[{
    _id:"...",
    body:{
        "_id":"...",
        "name" : "Blaster",
        "team" : "Autobots",
        "class" : "Commmunicator"
        "alt-mode" : "Sterio"
    }
},
{
    _id:"...",
    body:{
        "_id":"...",
        "name" : "Hotrod",
        "team" : "Autobots",
        "class" : "Warrior"
        "alt-mode" : "Car"
    }
}]

我想要的结果如下所示

[
    {
        "_id":"...",
        "name" : "Blaster",
        "team" : "Autobots",
        "class" : "Commmunicator"
        "alt-mode" : "Sterio"
    },
    {
        "_id":"...",
        "name" : "Hotrod",
        "team" : "Autobots",
        "class" : "Warrior"
        "alt-mode" : "Car"
    }
]

我的问题

如何实现我想要的结果结构。

1 个答案:

答案 0 :(得分:3)

所以,你会讨厌这个,但这只是聚合管道中$project$group阶段的工作方式。您需要在“显式”输出中指定所需的所有字段。因此:

feed.aggregate([
    {'$match':{_id:id('550234d3d06039d507d238d8')}},
    {'$unwind':'$body'},
    {'$skip':2},
    {'$limit':2},
    {
        '$project': {
            '_id':"$body._id",
            'name': '$body.name',
            'team': '$body.team',
            'class': '$body.alt-mode'
        }
    },
], function(err, result){

这确实是唯一的方法。

但实际上有一个强有力的推理,并且大多数原则都在SQL SELECT中备份,但适用的*“通配符”除外。

一般的前提是,这与一般操作中的unix pipe类似,所以如果您认为:

grep | awk | sed

然后,这些操作在结构上似乎更合乎逻辑。