【问题标题】:Mongoose structure aggregation output without field names没有字段名称的 Mongoose 结构聚合输出
【发布时间】:2015-05-18 13:05:39
【问题描述】:

我正在使用聚合对子文档中的数据进行分页。子文档没有严格的架构,因此每个文档可能不同,这使我难以构建输出,因为我不知道子文档的字段名称。

我正在使用什么

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"
    }
]

我的问题

我怎样才能达到我想要的结果结构。

【问题讨论】:

  • 结构良好的问题。

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


【解决方案1】:

所以,你会讨厌这个,但这正是聚合管道中 $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

然后这些操作在结构上看起来更合乎逻辑。

【讨论】:

  • 谢谢伙计,我很担心 =) 我目前的工作是将其添加到回调中: var newResult = []; result.forEach(function(item){ newResult.push(item.body); });
  • @PaulParton 就是这样,而且真的很有意义。根据编辑考虑“unix管道”。这里也没有“通配符”运算符。你得到你想要的。
猜你喜欢
  • 2021-08-26
  • 2017-04-26
  • 2021-05-15
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
相关资源
最近更新 更多