node mongoDb 根据key查询嵌套数组
node mongoDb query nested array based on key
我一直在尝试从MongoDB中的数据中根据键值在数组中找到一个对象,下面是从数据库中查询,但我只需要一些特定的对象,如何可以实现吗?
[
{
"_id": "627261a17acf7875b30d6e34",
"orderItem": [
[
{
"_id": "626fdb9fd0867e51d613530d",
"productName": "Handmade jewelry",
"productPrice": "25.99",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productimg4.jpg-1651497883882",
"sellerId": "62470b37f2052b6a6463f9b3",
"qty": 1
},
{
"_id": "626fdbf4d0867e51d6135389",
"productName": "Handmade bucket",
"productPrice": "23.99",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/topSellingImg.jpg-1651497970510",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823", "sellerId": "62470b37f2052b6a6463f9b3",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "62470b37f2052b6a6463f9b3",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b45",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "62470b37f2052b6a64637788",
"qty": 2
},
],
],
"__v": 0
}
]
这是我来自节点的代码示例
router.get("/orders", async (req, res) => {
const { sellerID } = req.query;
try {
const order = await SellerOrder.find({
sellerId: sellerID ,
});
res.status(200).json(order);
} catch (error) {
res.status(500).json(error + "error fetching data");
}
});
对于过滤器,你可以试试嵌套的$elemMatch
运算符,因为你的数据有嵌套数组,
尝试聚合运算符在投影中过滤数组,您也可以在投影中添加所需的字段,
$reduce
迭代orderItem
的循环,将初始值设置为空白数组
$filter
迭代 first-level 数组的循环以检查 sellerId
和过滤数组
$concatArrays
将当前数组与过滤后的数组连接起来
const order = await SellerOrder.find({
orderItem: {
$elemMatch: {
$elemMatch: {
sellerId: sellerID
}
}
}
},
{
orderItem: {
$reduce: {
input: "$orderItem",
initialValue: [],
in: {
$concatArrays: [
{
$filter: {
input: "$$this",
cond: {
$eq: ["$$this.sellerId", sellerID]
}
}
},
"$$value"
]
}
}
}
// add other projection fields here
})
我一直在尝试从MongoDB中的数据中根据键值在数组中找到一个对象,下面是从数据库中查询,但我只需要一些特定的对象,如何可以实现吗?
[
{
"_id": "627261a17acf7875b30d6e34",
"orderItem": [
[
{
"_id": "626fdb9fd0867e51d613530d",
"productName": "Handmade jewelry",
"productPrice": "25.99",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productimg4.jpg-1651497883882",
"sellerId": "62470b37f2052b6a6463f9b3",
"qty": 1
},
{
"_id": "626fdbf4d0867e51d6135389",
"productName": "Handmade bucket",
"productPrice": "23.99",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/topSellingImg.jpg-1651497970510",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823", "sellerId": "62470b37f2052b6a6463f9b3",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "62470b37f2052b6a6463f9b3",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b45",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "62470b37f2052b6a64637788",
"qty": 2
},
],
],
"__v": 0
}
]
这是我来自节点的代码示例
router.get("/orders", async (req, res) => {
const { sellerID } = req.query;
try {
const order = await SellerOrder.find({
sellerId: sellerID ,
});
res.status(200).json(order);
} catch (error) {
res.status(500).json(error + "error fetching data");
}
});
对于过滤器,你可以试试嵌套的$elemMatch
运算符,因为你的数据有嵌套数组,
尝试聚合运算符在投影中过滤数组,您也可以在投影中添加所需的字段,
$reduce
迭代orderItem
的循环,将初始值设置为空白数组$filter
迭代 first-level 数组的循环以检查sellerId
和过滤数组$concatArrays
将当前数组与过滤后的数组连接起来
const order = await SellerOrder.find({
orderItem: {
$elemMatch: {
$elemMatch: {
sellerId: sellerID
}
}
}
},
{
orderItem: {
$reduce: {
input: "$orderItem",
initialValue: [],
in: {
$concatArrays: [
{
$filter: {
input: "$$this",
cond: {
$eq: ["$$this.sellerId", sellerID]
}
}
},
"$$value"
]
}
}
}
// add other projection fields here
})