MongoDB-JSON- 如何在数组中进行查询?
MongoDB-JSON- How to make a query inside an array?
我有一个包含 20 个文档的集合,每个文档都有一个数组。
我想知道如何搜索文档中的排列
它看起来像:
"exist" : 66658,
"warehouse" : [
{
"stock_I" : "available",
"prod_st" : "active",
"last_sale" : "2022-11-23",
"factura_uv" : 154368244,
"price" : 8
},
{
"stock_II" : "available",
"prod_st" : "active",
"last_sale" : "2022-04-18",
"factura_uv" : 879624124,
"price" : 8
},
{
"stock_III" : "available",
"prod_st" : "active",
"last_sale" : "2021-07-05",
"factura_uv" : 357846988,
"price" : 8
}
],
我想要做的是能够 运行 一个命令,它只显示 stock_II
中可用的产品
我的mongo版本是5.0.4
感谢您的帮助
db.collection.aggregate([
{
$match: {
"warehouse.stock_II": "available"
}
},
{
$set: {
warehouse: {
$filter: {
input: "$warehouse",
as: "w",
cond: { $eq: [ "$$w.stock_II", "available" ] }
}
}
}
}
])
db.collection.find({
"warehouse.stock_II": "available"
},
{
"warehouse.$": 1
})
我有一个包含 20 个文档的集合,每个文档都有一个数组。 我想知道如何搜索文档中的排列 它看起来像:
"exist" : 66658,
"warehouse" : [
{
"stock_I" : "available",
"prod_st" : "active",
"last_sale" : "2022-11-23",
"factura_uv" : 154368244,
"price" : 8
},
{
"stock_II" : "available",
"prod_st" : "active",
"last_sale" : "2022-04-18",
"factura_uv" : 879624124,
"price" : 8
},
{
"stock_III" : "available",
"prod_st" : "active",
"last_sale" : "2021-07-05",
"factura_uv" : 357846988,
"price" : 8
}
],
我想要做的是能够 运行 一个命令,它只显示 stock_II
中可用的产品我的mongo版本是5.0.4
感谢您的帮助
db.collection.aggregate([
{
$match: {
"warehouse.stock_II": "available"
}
},
{
$set: {
warehouse: {
$filter: {
input: "$warehouse",
as: "w",
cond: { $eq: [ "$$w.stock_II", "available" ] }
}
}
}
}
])
db.collection.find({
"warehouse.stock_II": "available"
},
{
"warehouse.$": 1
})