Mongoose: $lookup is not working using mongoose and node but its working in mongo shell

Created on 20 Mar 2017  Â·  14Comments  Â·  Source: Automattic/mongoose

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.

Most helpful comment

Hey it's working you have to use the collection name which is in mongo...
Not the model name which you definend in node

For example
Suppose you have defined
Var User=require('./models/user')

Here collection name is User but in mongo shell collection name would be users

So use users instead of User
Hope you get this

All 14 comments

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

closing this since it's a duplicate of https://github.com/Automattic/mongoose/issues/5089

how to work $lookup in mean aplication ?

Hey it's working you have to use the collection name which is in mongo...
Not the model name which you definend in node

For example
Suppose you have defined
Var User=require('./models/user')

Here collection name is User but in mongo shell collection name would be users

So use users instead of User
Hope you get this

Thank u for the reply.

On Mon, Jul 24, 2017 at 7:14 AM, monasingh27294 notifications@github.com
wrote:

Hey it's working you have to use the collection name which is in mongo...
Not the model name which you definend in node

For example
Suppose you have defined
Var User=require('./models/user')

Here collection name is User but in mongo shell collection name would be
users

So use users instead of User
Hope you get this

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/Automattic/mongoose/issues/5090#issuecomment-317299547,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AXljnXZVFyJj1maMYwy-0Ohm2BfjjWLHks5sQ_b_gaJpZM4MimxB
.

its worked for you?

I was dealing with the same problem and solved it using the collection name which is in mongo in the "from" field, as @monasingh27294 said.

Even when this solves the problem, shouldn't mongoose make this name conversion?

yeah... but mongoose is not making this conversion

I dont know why
but it should be...

@monasingh27294 , I think it shouldn't, $lookup is mongodb related feature, not mongoose. So it introduce unnecessary complexity and confusion, in my opinion.

db.userprofile.aggregate ([
"$ lookup": {
"from": "transaction",
"localField": "uid",
"foreignField": "uid",
"as": "result"
}
])

Usa esto:

preguntaModel.aggregate([
{
$lookup: {
"from": "transaction",
"localField": "uid",
"foreignField": "uid",
"as": "result"
}
}])

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

I am facing the same issue, i am also using the mongoose.createConnection() for connecting db.
Please reply if anyone knows about it.

@raju2dev please provide some code samples. Also, make sure you're pluralizing the from collection correctly. You need to pass the collection name to from, not the model name

In case of mongoose(v5.6), use POPULATE. here it is

Was this page helpful?
0 / 5 - 0 ratings