$lookup working in mongo shell but not in mongoose. i am using mongo version 3.2.11 and mongoose version 4.9.1
Do you want to request a feature or report a bug?
What is the current behavior?
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
Please mention your node.js, mongoose and MongoDB version.
db.userprofile.aggregate([ "$lookup": { "from": "transaction", "localField": "uid", "foreignField": "uid", "as": "result" } ])
its working in mongo shell but not in node using mongoose i am getting empty array of result not getting data of another collection(transaction)
using in node as db.userprofile.aggregate([ "$lookup": { "from": "transaction", //collection name "localField": "uid", // uid is exists in both collection "foreignField": "uid", "as": "result" } ]).exec().then(function(data){ console.log(data) }).catch(function(err){ console.log(err) }) not working getting empty array of result
This works for me:
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const co = require('co');
const GITHUB_ISSUE = `gh-5089`;
exec()
.then(() => {
console.log(`Program ran successfully`);
process.exit(0);
})
.catch(error => {
console.error(`Error: ${ error }\n${ error.stack }`);
process.exit(2);
});
function exec() {
return co(function*() {
const db = mongoose.createConnection(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);
const orderSchema = new mongoose.Schema({
item: String,
price: Number,
quantity: Number
});
const Order = db.model('Order', orderSchema);
yield Order.remove({});
yield Order.create([
{ item: 'abc', price: 12, quantity: 2 },
{ item: 'jkl', price: 20, quantity: 1 }
]);
const inventorySchema = new mongoose.Schema({
sku: String,
description: String,
inStock: Number
});
const Inventory = db.model('Inventory', inventorySchema);
yield Inventory.remove({});
yield Inventory.create([
{ sku: 'abc', description: 'product 1', inStock: 120 },
{ sku: 'def', description: 'product 2', inStock: 80 },
{ sku: 'ghi', description: 'product 3', inStock: 60 },
{ sku : 'jkl', description: 'product 4', inStock : 70 },
{ sku: null, description: 'Incomplete' }
]);
const orders = yield Order.aggregate([
{
$lookup: {
from: 'inventories',
localField: 'item',
foreignField: 'sku',
as: 'inventoryDocs'
}
}
]);
console.log(orders);
});
}
This logs:
[ { _id: 58d4734dac3df117c33b3108,
item: 'abc',
price: 12,
quantity: 2,
__v: 0,
inventoryDocs: [ [Object] ] },
{ _id: 58d4734dac3df117c33b3109,
item: 'jkl',
price: 20,
quantity: 1,
__v: 0,
inventoryDocs: [ [Object] ] } ]
Make sure your from field is referring to the correct collection name. From what you've posted, it might be transactions but your repro example is pretty incomplete. Also it looks like you're using the mongo node driver and not mongoose itself?
I have these two schemes. How to join them?
schema/trip.js
`import mongoose from 'mongoose';
const schema = mongoose.Schema({
day: {type: Number, required: true},
hour: {type: String, required: true},
uuid: {type: String, required: true},
destiny: {type: String, required: true},
dayOperation: {type: String, required: true},
_line: {
_id: {type: String, required: true},
name: {type: String, required: true},
code: {type: String, required: true},
router: {
_id: {type: String, required: false},
name: {type: String, required: false}
},
},
createdAt: {type: Date, default: Date.now},
finishAt: {type: Date, required: false},
conclusion: {
success: {type: Boolean, required: false, default: false},
description: {type: String, required: false, default: 'Fechado sem resposta.'},
}
});
schema/location.js
export default mongoose.model('Trip', schema);
`
`import mongoose from 'mongoose';
const schema = mongoose.Schema({
trip_id:{
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'trip'
},
locations: [{
activity: {
confidence: {type: Number, required: true},
type: []
},
coords: {
accuracy: {type: Number, required: true},
altitude: {type: Number, required: true},
heading: {type: Number, required: true},
latitude: {type: Number, required: true},
longitude: {type: Number, required: true},
speed: {type: Number, required: true}
},
is_moving: {type: Boolean, required: true},
odometer: {type: Number, required: true},
event: {type: String, required: false},
distanceFilter: {type: Number, required: true},
createdAt: { type: Date, default: Date.now }
}]
}, {timestamps: true});
export default mongoose.model('Location', schema);`
and I have my route
routes/trip.js
router.get('/by-car', filter, (req, res) => {
}
Use mongoose populate() or https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/