MST
星途 面试题库

面试题:MongoDB where查询中的复杂嵌套逻辑实现

假设有一个MongoDB集合,文档结构如下:{ 'user': { 'name': 'string', 'age': 'number', 'hobbies': ['string'] }, 'orders': [ { 'orderId': 'string', 'amount': 'number', 'status': 'string' } ] }。要求查询出用户年龄大于25,并且至少有一个订单状态为 'completed' 且订单金额大于100的所有文档。请写出对应的查询语句。
23.7万 热度难度
数据库MongoDB

知识考点

AI 面试

面试题答案

一键面试
db.collection.find({
    "user.age": { $gt: 25 },
    "orders": {
        $elemMatch: {
            "status": "completed",
            "amount": { $gt: 100 }
        }
    }
});