Skip to content

Instantly share code, notes, and snippets.

@GlauberF
Last active February 5, 2022 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GlauberF/cf702ef66c31da9951966322347f426a to your computer and use it in GitHub Desktop.
Save GlauberF/cf702ef66c31da9951966322347f426a to your computer and use it in GitHub Desktop.
get real object in for_id_supplier
// Requires official MongoShell 3.6+
db = db.getSiblingDB("....");
db.getCollection("produtos").aggregate(
[
{
"$project" : {
"_id" : NumberInt(0),
"produtos" : "$$ROOT"
}
},
{
"$lookup" : {
"localField" : "produtos.uni_id_unidademedida",
"from" : "unidades_medidas",
"foreignField" : "_id",
"as" : "uni_id_unidademedida"
}
},
{
"$unwind" : {
"path" : "$uni_id_unidademedida",
"preserveNullAndEmptyArrays" : true
}
},
{
"$lookup" : {
"localField" : "produtos.tpi_id_tipoitem",
"from" : "tipo_item",
"foreignField" : "_id",
"as" : "tpi_id_tipoitem"
}
},
{
"$unwind" : {
"path" : "$tpi_id_tipoitem",
"preserveNullAndEmptyArrays" : true
}
},
{
"$lookup" : {
"localField" : "produtos.cat_id",
"from" : "categorias",
"foreignField" : "_id",
"as" : "cat_id"
}
},
{
"$unwind" : {
"path" : "$cat_id",
"preserveNullAndEmptyArrays" : true
}
},
{
"$lookup" : {
"localField" : "produtos.nat_id",
"from" : "natureza_operacao",
"foreignField" : "_id",
"as" : "nat_id"
}
},
{
"$unwind" : {
"path" : "$nat_id",
"preserveNullAndEmptyArrays" : true
}
},
{
"$lookup" : {
"localField" : "produtos.emb_id_embalagem",
"from" : "emb_id_embalagem",
"foreignField" : "_id",
"as" : "emb_id_embalagem"
}
},
{
"$unwind" : {
"path" : "$emb_id_embalagem",
"preserveNullAndEmptyArrays" : true
}
},
{
"$lookup" : {
"localField" : "produtos.pro_fornecedores.for_id_fornecedor",
"from" : "pessoas",
"foreignField" : "_id",
"as" : "pessoas"
}
},
{
"$unwind" : {
"path" : "$pessoas",
"preserveNullAndEmptyArrays" : true
}
},
{
"$match" : {
"produtos._id" : ObjectId("61688d314d22e02fd019e125"),
"produtos.data_exclusao" : null,
"produtos.emp_id" : ObjectId("5c0ea82429073937c80c1594")
}
},
{
"$project" : {
"produtos._id" : "$produtos._id",
"produtos.pro_descricao" : "$produtos.pro_descricao",
"produtos.pro_fornecedores" : "$produtos.pro_fornecedores",
"uni_id_unidademedida._id" : "$uni_id_unidademedida._id",
"uni_id_unidademedida.uni_codigo" : "$uni_id_unidademedida.uni_codigo",
"uni_id_unidademedida.uni_descricao" : "$uni_id_unidademedida.uni_descricao",
"uni_id_unidademedida.uni_fragmenta" : "$uni_id_unidademedida.uni_fragmenta",
"tpi_id_tipoitem._id" : "$tpi_id_tipoitem._id",
"tpi_id_tipoitem.tpi_codigo" : "$tpi_id_tipoitem.tpi_codigo",
"tpi_id_tipoitem.tpi_descricao" : "$tpi_id_tipoitem.tpi_descricao",
"cat_id._id" : "$cat_id._id",
"cat_id.cat_nome" : "$cat_id.cat_nome",
"cat_id.cat_descricao" : "$cat_id.cat_descricao",
"nat_id._id" : "$nat_id._id",
"nat_id.nat_descricao" : "$nat_id.nat_descricao",
"emb_id_embalagem._id" : "$emb_id_embalagem._id",
"emb_id_embalagem.emb_descricao" : "$emb_id_embalagem.emb_descricao",
"_id" : NumberInt(0)
}
}
],
{
"allowDiskUse" : true
}
);
// Current results
{
"produtos" : {
"_id" : ObjectId("61688d314d22e02fd019e125"),
"pro_descricao" : "**195/55R15 BRIDG ECOPIA EP150 85H",
"pro_fornecedores" : [
{
"pro_cod_fornecedor" : "B11590",
"for_id_fornecedor" : ObjectId("613a5757b3cbed71027c53d6"),
"pro_conv_for" : NumberInt(1)
},
{
"pro_cod_fornecedor" : "B11590",
"for_id_fornecedor" : ObjectId("5c12a3da290739638077ab56"),
"pro_conv_for" : NumberInt(1)
}
]
},
"uni_id_unidademedida" : {
"_id" : ObjectId("59fdb9890028ce2ba9066fc3"),
"uni_codigo" : "UN",
"uni_descricao" : "UNIDADE",
"uni_fragmenta" : true
},
"tpi_id_tipoitem" : {
"_id" : ObjectId("598391880028ce2acb046ed6"),
"tpi_codigo" : "00",
"tpi_descricao" : "Mercadoria para revenda \"Adquiridas de Terceiros\""
},
"cat_id" : {
},
"nat_id" : {
},
"emb_id_embalagem" : {
}
}
// How do I need you to return.
{
"produtos" : {
"_id" : ObjectId("61688d314d22e02fd019e125"),
"pro_descricao" : "**195/55R15 BRIDG ECOPIA EP150 85H",
"pro_fornecedores" : [
{
"pro_cod_fornecedor" : "B11590",
"for_id_fornecedor" : {
"pes_nome": "Name"
},
"pro_conv_for" : NumberInt(1)
},
{
"pro_cod_fornecedor" : "B11590",
"for_id_fornecedor" : {
"pes_nome": "Name 2"
},
"pro_conv_for" : NumberInt(1)
}
]
},
"uni_id_unidademedida" : {
"_id" : ObjectId("59fdb9890028ce2ba9066fc3"),
"uni_codigo" : "UN",
"uni_descricao" : "UNIDADE",
"uni_fragmenta" : true
},
"tpi_id_tipoitem" : {
"_id" : ObjectId("598391880028ce2acb046ed6"),
"tpi_codigo" : "00",
"tpi_descricao" : "Mercadoria para revenda \"Adquiridas de Terceiros\""
},
"cat_id" : {
},
"nat_id" : {
},
"emb_id_embalagem" : {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment