MST
星途 面试题库

面试题:MongoDB多条件嵌套查询及投影操作

在MongoDB的`orders`集合中,文档结构为{order_id: 1, customer: 'Alice', items: [{product: 'Apple', quantity: 5, price: 2}, {product: 'Banana', quantity: 3, price: 1}], total_amount: 13}。要求查询出所有订单中购买了苹果且购买数量大于3,同时只返回订单号、顾客名和总金额的文档。
33.3万 热度难度
数据库MongoDB

知识考点

AI 面试

面试题答案

一键面试
db.orders.find(
    {
        "items.product": "Apple",
        "items.quantity": { $gt: 3 }
    },
    {
        order_id: 1,
        customer: 1,
        total_amount: 1,
        _id: 0
    }
)