我有这份文件
{
_id: ObjectId('5f8970f19e6afb628a2edd07'),
name: 'pack 1',
price: 100,
min_price: 100,
updated_at: ISODate('2020-10-16T10:07:45.570Z'),
created_at: ISODate('2020-10-16T10:07:45.530Z'),
available_quantity: -1,
quantity: -1,
slots: [
{
duration: '365',
price: 100,
_id: '5f8970f19e6afb628a2edd07_5f8970f18b3aa',
created_at: ISODate('2020-10-16T10:07:45.529Z'),
updated_at: ISODate('2020-10-16T10:07:45.529Z'),
available_quantity: -1,
quantity: -1
},
{
duration: '255',
_id: '5f8970f19e6afb628a2edd07_5f8970f18b3c7',
created_at: ISODate('2020-10-16T10:07:45.529Z'),
updated_at: ISODate('2020-10-16T10:07:45.529Z'),
price: 100,
available_quantity: -1,
quantity: -1
}
]
}我只希望这个子文档使用Moloquent
{
duration: '365',
price: 100,
_id: '5f8970f19e6afb628a2edd07_5f8970f18b3aa',
created_at: ISODate('2020-10-16T10:07:45.529Z'),
updated_at: ISODate('2020-10-16T10:07:45.529Z'),
available_quantity: -1,
quantity: -1
}搜索必须由_id在subDocument上完成,例如。_id: 5f8970f19e6afb628a2edd07_5f8970f18b3aa
发布于 2020-10-20 23:11:49
以下是mongo查询。
db.collection.aggregate([
{
"$unwind": "$slots"
},
{
"$match": {
"slots._id": "5f8970f19e6afb628a2edd07_5f8970f18b3aa"
}
},
{
"$project": {
_id: 0,
price: "$slots.price",
duration: "$slots.duration",
created_at: "$slots.created_at",
updated_at: "$slots.updated_at",
_id: "$slots._id",
available_quantity: "$slots.available_quantity",
quantity: "$slots.quantity"
}
}
])还在这里创建了一个Mongo游乐场。
https://stackoverflow.com/questions/64423669
复制相似问题