Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb $lookup give [] #21

Open
BLACKJACKDEVELOPER opened this issue Aug 3, 2023 · 2 comments
Open

Mongodb $lookup give [] #21

BLACKJACKDEVELOPER opened this issue Aug 3, 2023 · 2 comments

Comments

@BLACKJACKDEVELOPER
Copy link

Here is my code that attempt to use $lookup.I think it's correct but "UserDetail" give me a [] to all data

async get(req,res) {
		try {
			const { query:{ room } } = req
			const data = await msgs.aggregate([
  			{
    			$lookup: {
      				from: "users", localField: "user_id", foreignField: "id", as: "UserDetail"
    		}
  		}
	]);
			console.log(data)
			return res.json({
				data,
				pass:true
			})
		}catch(e) {
			console.log(e)
			return res.json({
				pass:false,
				msg:e.message
			})
		}
	}
	
@dhruvinjs
Copy link

hey so am new here so can u assign me this issue here

@dhruvinjs
Copy link

async get(req,res) {
try {
const { query:{ room } } = req
const data = await msgs.aggregate([
{
$lookup: {
from: "users", localField: "user_id", foreignField: "_id", as: "UserDetail"
}
}
]);
console.log(data)
return res.json({
data,
pass:true
})
}catch(e) {
console.log(e)
return res.json({
pass:false,
msg:e.message
})
}
}
hey i think there is a typo error in your code assuming if u want to match the id which I think is ObjectId so just have to change "id" to "_id"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants