在localhost上工作的Mongo $查找在heroku部署中停止工作

时间:2016-04-05 02:07:07

标签: express heroku mongoose aggregate mlab

我使用heroku来部署我的mongoose应用程序,而以下聚合返回本地mhost服务器运行的localhost上的正确数据,它在heroku服务器上返回空结果,mongolab用于处理数据库。

这是查询本身:

let projects = this.models.Project.aggregate(
[
    {
        $lookup: {from: 'users', localField: 'creator', foreignField: '_id', as: 'creator'}
    },

    { $unwind : "$creator" },

    {
        $project: {
            name: 1,
            updated: { $dateToString: { format: "%Y-%m-%d", date: "$updated" } },
            creator: 1,
            description: 1
        }
    }
]
)
.exec((err, projects) => {
console.log("fetched projects is this:", projects)
res.send(projects);
});

以下是用户和项目架构:

    let userSchema = new Schema({
        _id: Schema.Types.ObjectId,
        username: String,
        password: String,
        email: String,
        gender: String,
        address: String
    });

    let projectSchema = new Schema({
        name: String,
        updated: { type: Date, default: Date.now },
        creator: {type: Schema.Types.ObjectId, ref: 'User'},
        description: String,
        issues: [{type: Schema.Types.ObjectId, ref: 'Issue'}]
    });

与mongolab服务器的连接已正确配置并正常工作,我能够在那里创建用户和项目实例。

我认为问题的核心是我的localhost中的文档ID如下所示:

"_id" : ObjectId("56f1ace9e72b71643070e9a0")

在mongolab中就像这样:

"_id": {
    "$oid": "57030f0972365f0300c56c88"
}

感谢您提前提供任何帮助)

2 个答案:

答案 0 :(得分:1)

mLab(以前称为MongoLab)尚不支持3.2 - $lookup是此版本中的新功能。

http://docs.mlab.com/ops/#version-mgmt

答案 1 :(得分:0)

最终我想出了这个适用于当前MongoLab的替代方案:

resources.app.get('/projectsItems', (req, res) => {
    let projects = this.models.Project.aggregate(
        [
            {
                $project: {
                    name: 1,
                    updated: { $dateToString: { format: "%Y-%m-%d", date: "$updated" } },
                    creator: 1,
                    description: 1
                }
            }
        ]
    )
    .exec((err, projects) => {
        this.models.Project.populate(projects, {path: "creator"}, (err, projects) => {
            console.log("fetched projects is this:", projects)
            res.send(projects);
        });
    });
});

有关其工作原理的详细信息,请查看here