如何使用Loopback框架查询嵌套字段?

时间:2019-07-16 13:56:50

标签: mongodb-query loopbackjs

我已经使用MongoDB在一个在线市场上设置了Loopback 4 API,并创建了一个模型来保存商店的产品报价(产品折扣特价)。该型号包含报价信息和产品信息。产品信息包含正在销售此产品的商店信息(例如商店ID)。这是样本数据的样子:

{
    "_id" : ObjectId("5d27e3b19eb1641840562560"),
    "createdAt" : 1561509568,
    "updatedAt" : 1561509568,
    "cashBackPerVisit" : {
        "amount" : 1000,
        "precision" : 2,
        "currency" : "USD",
        "symbol" : "$"
    },
    "conditions" : [ 
        "NO RETURNS"
    ],
    "description" : "Beautiful printed t-shirt with your favorite superhero!",
    "maxQuantity" : 2,
    "maxVisits" : 1,
    "merchantUID" : "re5OljVl3KObKLopKdxpOQBWPL82",
    "startDate" : 1561509568,
    "expiration" : 1561509569,
    "title" : "$10 cashback on t-shirt",
    "product" : {
        "_id" : "5d27dcba9eb1641840562272",
        "imageUrl" : "https://via.placeholder.com",
        "merchantUID" : "re5OljVl3KObKLopKdxpOQBWPL82",
        "attributes" : {
            "sku" : "2169049"
        },
        "_tags" : [ 
            "t-shirt", 
            "pique", 
            "shirts"
        ],
        "category" : {
            "_id" : "5d12bf519eb1641840519334",
            "createdAt" : 1561509568,
            "updatedAt" : 1561509568,
            "name" : "Apparel, shoes, jewelry",
            "commissionRate" : 5,
            "subCategories" : [ 
                "Accessories", 
                "Apparel", 
                "Jewelry & watches", 
                "Shoes"
            ]
        },
        "name" : "Adult Pique Sport Shirt - Ash - 2XL Case Pack 12",
        "description" : "Preshrunk pique knit. Satin label. 3 woodtone buttons. Contoured welt collar and cuffs. Double needle bottom hem. Quarter-turned to eliminate center crease. Clean finished placket with reinforced bottom box.",
        "price" : {
            "amount" : 2000,
            "precision" : 2,
            "currency" : "USD",
            "symbol" : "$"
        },
        "store" : {
            "_id" : "5d27d9af9eb1641840562169",
            "companyName" : "Steve's t-shirt shack",
            "emails" : [ 
                "boom-merchant-test@hotmail.com"
            ],
            "address" : "111 Belvediere Dr.",
            "latitude" : 26.7153,
            "longitude" : 80.0534,
            "merchant" : {
                "uid" : "re5OljVl3KObKLopKdxpOQBWPL82",
                "name" : ""
            }
        }
    }
}

我想从我的API端点查询特定商店ID中的所有商品。

所以我写了这个:

获取/api/v1/offers?filter[where][product.store._id]=5d27d9af9eb1641840562169

这将返回一个空数组。

如果我只执行GET / offers,我会看到带有该ID的商店的报价。所以我不确定我在做什么错。

奇怪的是,如果我写了,我也找不到产品ID:

获取/api/v1/offers?filter[where][product._id]=5d27dcba9eb1641840562272

但是,如果我查询商品ID,我会找到该商品:

获取/api/v1/offers?filter[where][_id]=5d27e3b19eb1641840562560

为什么我的其他查询找不到我的嵌套字段?我的商品控制器是使用默认的生成器设置生成的,所以我认为那里没有配置错误。

0 个答案:

没有答案