Node.js 如何使用mongoose(express、nodejs)连接两个表(文档)并获取不匹配的数据?
我有两个表(文档)报价和应用报价,我需要合并并从报价表中获取不匹配的数据。 例如,我在数据库中存储了一些对象。我正在为此使用meanjs我不知道我在哪里可以为连接两个表(文档)编写路由,我在哪里可以为连接编写函数请有人帮助我完成这项工作Node.js 如何使用mongoose(express、nodejs)连接两个表(文档)并获取不匹配的数据?,node.js,mongodb,join,express,mongoose,Node.js,Mongodb,Join,Express,Mongoose,我有两个表(文档)报价和应用报价,我需要合并并从报价表中获取不匹配的数据。 例如,我在数据库中存储了一些对象。我正在为此使用meanjs我不知道我在哪里可以为连接两个表(文档)编写路由,我在哪里可以为连接编写函数请有人帮助我完成这项工作 $scope.offers = [{ id: "1", storeid: "986745", couponname: "healthy breakfast offer", offermessage
$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"
}
]
例如,如果我们放置连接并获取不匹配的数据,那么在两个表中,这里应该只显示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();
});
};
适用的优惠路线:
"
应用报价控制器:
/**
*Appliedoffer中间件
*/
我会在数据库端这样做,而且效率会更高 自从MongoDB(3.2)的最新版本以来,可以进行$lookUp,这实际上是一个左外连接 从mongodb.org下载最新版本的mongodb,并在此处查看$lookUp文档以执行跨集合的连接:
根据mongolab的支持页面,他们目前只运行mongodb 3.0:您可能需要等待3.2。我如何解决我的问题。有什么办法可以替代吗
'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);
};
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();
});
};