Hi I’m trying to make a query to mongo where I need to get an array of elements which is inside a collection’s property, I have the following in my code:
func (p *ProductMongo) Get(id bson.ObjectId, limit, skip int) ([]*Product, error) {
query := []bson.M{
{"$match": bson.M{"_id": id}},
{"$project": bson.M{"products": bson.M{"$slice": []interface{}{"$shop.products", skip, limit}}, "_id": 0}},
}
var products []*Product
err := p.DB(d.Databases["db_sites"]).C("sites").Pipe(query).All(&products)
if err != nil {
return nil, err
}
return products, nil
}
It doesn’t give me an error but when I print the slice products it’is empty I dont know why.
This is my collection:
{
"_id" : ObjectId("592ed0834c96f81708f826bc"),
"owner" : ObjectId("592ed0834c96f81708f826bd"),
"domain" : "",
"server" : "",
"wh_client_id" : "",
"license" : "DEMO",
"about" : "all",
"template" : "592466ea6df79a254b58f464",
"plan" : "",
"type" : "commercial",
"wizard" : 9,
"maindata" : {
"identity" : {
"logo" : "87c1suIGBuUbmibIR3CUH2e2Xlw=.gif",
"title" : "Título de mi sitio",
"slogan" : "Eslogan de mi sitio"
},
"company" : null,
"contact" : {
"email" : "mail@mail.com",
"address" : null,
"tels" : [],
"gmap" : {
"x" : 19.3902835845947,
"y" : -99.2835235595703,
"addr" : "Calle Clemente Rueda 9, San Fernando, 52765 Naucalpan de Juárez, Méx., Mexico"
},
"schedules" : [],
"is" : ""
},
"social" : null
},
"templatedata" : {
"menus" : [],
"slides" : [
{
"title" : "Título del primer producto estrella",
"description" : "Descripción del producto estrella",
"img" : "nOYF9HEL3ev2s-YXEdKsA-Z3mnI=.png"
},
{
"title" : "Título del segundo producto estrella",
"description" : "Descripción del producto estrella",
"img" : "f2B6V_f1lUEohl6hA_agHivpEWw=.png"
},
{
"title" : "Título del tercer producto estrella has changed",
"description" : "Descripción del producto estrella",
"img" : "bJddZIhz8q564__Y7-eFNIMsr3s=.png"
}
],
"features" : [
{
"title" : "Precio",
"icon" : "fa-price",
"img" : "",
"description" : "Tenemos buenos precios"
},
{
"title" : "Estrategia",
"icon" : "fa-strategy",
"img" : "",
"description" : "Contamos con la mejor estrategia"
},
{
"title" : "Calidad",
"icon" : "fa-quality",
"img" : "",
"description" : "Manejamos la mejor calidad"
}
],
"actions" : [
{
"title" : "Contáctanos",
"type" : "",
"icon" : "fa fa-file-text-o",
"description" : "Contáctenos!",
"link" : {
"active" : false,
"placeholder" : "",
"url" : ""
}
},
{
"title" : "Video",
"type" : "",
"icon" : "fa fa-video-camera",
"description" : "Ver video",
"link" : {
"active" : true,
"placeholder" : "URL",
"url" : ""
}
},
{
"title" : "Comprar",
"type" : "",
"icon" : "fa fa-shopping-cart",
"description" : "Compre ahora",
"link" : {
"active" : false,
"placeholder" : "",
"url" : ""
}
}
],
"footer" : []
},
"edit" : null,
"render" : null,
"shop" : {
"_id" : ObjectId("59402c614c96f85f65eae1cb"),
"title" : "Nombre de la tienda changed",
"description" : "Descripcion de la tienda",
"products" : [
{
"_id" : ObjectId("594836fa4c96f820fc535d40"),
"name" : "Playera azul",
"description" : "Esta playera tipo polo de color azul.",
"shortDescription" : "Playera tipo polo",
"images" : [],
"thumbnails" : [],
"price" : 300,
"stock" : 10,
"badge" : "",
"categoryID" : ObjectId("594813b54c96f879848aab7c")
},
{
"_id" : ObjectId("594837e74c96f824bea99988"),
"name" : "Pantalón de vestir",
"description" : "Este es un pantalón de vestir changed",
"shortDescription" : "Pantalón de vestir",
"images" : [],
"thumbnails" : [],
"price" : 880,
"stock" : 5,
"badge" : "",
"categoryID" : ObjectId("594813b54c96f879848aab7c")
},
{
"_id" : ObjectId("59484a7d4c96f8398bca4474"),
"name" : "Laptop Macbook Pro",
"description" : "Esta es la descripción de la Macbook pro 2016",
"shortDescription" : "Esta es la descripción corta",
"images" : [],
"thumbnails" : [],
"price" : 23800,
"stock" : 7,
"badge" : "",
"categoryID" : ObjectId("594848934c96f8398bca4473")
},
{
"_id" : ObjectId("594956684c96f83a3efe868f"),
"name" : "Laptop Asus",
"description" : "Computadora Asus i5 con 8GB de memoria RAM",
"shortDescription" : "Computadoa Asus",
"images" : [],
"thumbnails" : [],
"price" : 14999,
"stock" : 12,
"badge" : "peso",
"categoryID" : ObjectId("594956244c96f83a3efe868e")
}
],
"created" : ISODate("2017-05-31T14:17:39.389Z"),
"active" : false,
"public" : false
}