Skip to content

Instantly share code, notes, and snippets.

@josemiguelq
Created May 9, 2018 14:15
Show Gist options
  • Save josemiguelq/4dd73d50a16ceaf01ce323588009d9cd to your computer and use it in GitHub Desktop.
Save josemiguelq/4dd73d50a16ceaf01ce323588009d9cd to your computer and use it in GitHub Desktop.
Query join mongodb
db.sales.aggregate([
{
$lookup : {
from: "services",
localField: "_id",
foreignField: "saleId",
as:"servicesOfSale"
}
},
{
$match : {
"user.cpf" : "00000000"
}
}
])
//and the inverse
db.services.aggregate([
{
$lookup : {
from: "sales",
localField: "saleId",
foreignField:"_id",
as:"saleOfService"
}
},
{
$match : {
"saleOfService.user.cpf" : "000000000"
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment