如何使用mongoose(express,nodejs)连接两个表(文档)并获取不匹配的数据?

时间:2016-01-30 15:11:47

标签: node.js mongodb join express mongoose

我有两个表(文档)优惠和应用优惠我需要结合并从优惠表中获取无与伦比的数据。 例如,我已将一些对象存储在数据库中。我正在使用meanjs这个我不知道我在哪里可以写路由加入两个表(文件)和我在哪里可以写函数加入pls一些帮我完成这个

$scope.offers = [{

        id: "1",
        storeid: "986745",
        couponname: "healthy breakfast offer",
        offermessage: "50% offer for break fast",
        noofcoupon: "10"
    }, {
         id: "2",
        storeid: "886745",
        couponname: "get 50% lunch",
        offermessage: "50% offer for Lunch",
        noofcoupon: "10"
    }, {
         id: "3",
        storeid: "690745",
        couponname: "dinner damaka",
        offermessage: "50% offer for dinner",
        noofcoupon: "10"
    },
    {
         id: "4",
         storeid: "550745",
        couponname: "supper festiwal",
        offermessage: "80% offer for supper",
        noofcoupon: "10"
    },

    {
         id: "5",
         storeid: "690733",
        couponname: "eve damaka snaks",
        offermessage: "20% offer for snaks",
        noofcoupon: "10",
    }

    ] 

和应用商品表(文件)我已经存储了这样的数据

$scope.appliedoffers = [{

       id: "1",
        storeid: "986745",
        couponname: "healthy breakfast offer",
        offermessage: "50% offer for break fast",
        noofcoupon: "10",
    }, {
         id: "2",
         storeid: "690733",
        couponname: "eve damaka snaks",
        offermessage: "20% offer for snaks",
        noofcoupon: "10"
    }

    ]

对于给定的例子,如果我们放入join并得到不匹配的数据,它应该只显示3这里storeid有两个表

{
         id: "2",
        storeid: "886745",
        couponname: "get 50% lunch",
        offermessage: "50% offer for Lunch",
        noofcoupon: "10"
    }, {
         id: "3",
        storeid: "690745",
        couponname: "dinner damaka",
        offermessage: "50% offer for dinner",
        noofcoupon: "10"
    },
    {
         id: "4",
         storeid: "550745",
        couponname: "supper festiwal",
        offermessage: "80% offer for supper",
        noofcoupon: "10"
    },

我添加了我的路线和控制器我不知道如何获取无与伦比的数据。我已经添加了路由和控制器如何更改控制器以获得无与伦比的数据

提供路线

'use strict';

    module.exports = function(app) {
        var users = require('../../app/controllers/users.server.controller');
        var offers = require('../../app/controllers/offers.server.controller');

        // Offers Routes
        app.route('/offers')
            .get(offers.list)
            .post(users.requiresLogin, offers.create);

        app.route('/offers/:offerId')
            .get(offers.read)
            .put(users.requiresLogin, offers.hasAuthorization, offers.update)
            .delete(users.requiresLogin, offers.hasAuthorization, offers.delete);

        // Finish by binding the Offer middleware
        app.param('offerId', offers.offerByID);
    };

提供控制器:用于获取功能

/**
 * Offer middleware
 */
exports.offerByID = function(req, res, next, id) { 
    Offer.findById(id).populate('user', 'displayName').exec(function(err, offer) {
        if (err) return next(err);
        if (! offer) return next(new Error('Failed to load Offer ' + id));
        req.offer = offer ;
        next();
    });
};

已应用的优惠路线:

'use strict';

module.exports = function(app) {
    var users = require('../../app/controllers/users.server.controller');
    var appliedoffers = require('../../app/controllers/appliedoffers.server.controller');

    // Appliedoffers Routes
    app.route('/appliedoffers')
        .get(appliedoffers.list)
        .post(users.requiresLogin, appliedoffers.create);

    app.route('/appliedoffers/:appliedofferId')
        .get(appliedoffers.read)
        .put(users.requiresLogin, appliedoffers.hasAuthorization, appliedoffers.update)
        .delete(users.requiresLogin, appliedoffers.hasAuthorization, appliedoffers.delete);

    // Finish by binding the Appliedoffer middleware
    app.param('appliedofferId', appliedoffers.appliedofferByID);
};

应用报价控制器:

/ **  * Appliedoffer中间件  * /

exports.appliedofferByID = function(req, res, next, id) { 
    Appliedoffer.findById(id).populate('user', 'displayName').exec(function(err, appliedoffer) {
        if (err) return next(err);
        if (! appliedoffer) return next(new Error('Failed to load Appliedoffer ' + id));
        req.appliedoffer = appliedoffer ;
        next();
    });
};

1 个答案:

答案 0 :(得分:0)

我会在数据库方面这样做,并且效率更高。

自最新发布的MongoDB(3.2)以来,可以进行$ lookUp实际上是一个左外连接。

从mongodb.org下载最新版本的mongodb,并查看$ lookUp文档,以便在此处执行跨集合的连接:

https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/