MST

星途 面试题库

面试题:MongoDB复杂嵌套文档结构下结果集数据条数统计

有一个集合orders,文档结构如下:{_id: ObjectId('...'), customer: {name: 'John', address: {city: 'New York', street: '123 Main St'}}, orderItems: [{product: 'Product1', quantity: 2}, {product: 'Product2', quantity: 1}]}。现在要统计住在纽约且订单中包含Product1的订单数量,写出相应的MongoDB统计代码。
41.4万 热度难度
数据库MongoDB

知识考点

AI 面试

面试题答案

一键面试
db.orders.countDocuments({
    "customer.address.city": "New York",
    "orderItems.product": "Product1"
});