Reputation: 113
I am trying to make a query so that I only return the 'productos' that are on the current 'categoria', they also need to have the 'stock.cantidad' field greater or equal than 1 and the 'stock.idCedis' equal to a specific value, and this is how I am trying to do it:
return Productos.find(
{
idCategoria: Router.current().params._id,
"stock.cantidad":{$gte: 1},
"stock.idCedis":idCedis
});
I checked and the "stock.idCedis":idCedis is working just fine displaying the 'productos' that have that specific 'idCedis', but what I am having problems with is the "stock.cantidad":{$gte: 1}, part because I don't know why Meteor or Mongo DB for that matter are just ignoring it.
The schema for the stock part of 'productos' that I am currently using is this:
stock: {
type: [Object]
},
"stock.$.cantidad": {
type: Number,
label: "Cantidad de Stock",
min: 0
},
"stock.$.idCedis": {
type: String,
label: "Centro de Distribución"
},
So I would like to know if I am doing something wrong or any other way I could make this work.
Upvotes: 0
Views: 196
Reputation: 20227
Since stock.$.cantitad is an array try $elemMatch:
return Productos.find(
{
idCategoria: Router.current().params._id,
"stock.cantidad":{$elemMatch {$gte: 1}},
"stock.idCedis":idCedis
});
The mongo docs indicate that you shouldn't need to do this when there's only a single query condition but given how Meteor interacts with mongo I'd give it a try.
Upvotes: 1
Reputation: 113
Muchas gracias Michael Floyd, what I needed was that $elemMatch query operator, but for some reason your code did not worked form me, what worked was this:
Productos.find(
{
idCategoria: Router.current().params._id,
stock: { $elemMatch: { idCedis: idCedis, cantidad: { $gte: 0 } } }
});
Upvotes: 0