gpt4 book ai didi

mongodb - findOne 然后 findMany 使用在 findOne 中找到的文档中的值,在单个查询中

转载 作者:行者123 更新时间:2023-12-04 03:36:49 26 4
gpt4 key购买 nike

const categorySlug = req.query.category;
const category = await Category.findOne({ slug: categorySlug });
const children = await Category.find({ parent: category._id });

是否可以将这两个查询合并为一个?我试着做类似的事情

const children = await Category.aggregate([
{
$match: {
slug: categorySlug,
},
},
{
// somehow use the _id field from document fetched in first stage
}
]);

但我不知道这是否可能。

编辑:

const document = await Category.aggregate([
{
$match: {
slug: categorySlug,
},
},
{
$lookup: {
from: 'categories',
localField: '_id',
foreignField: 'parent',
as: 'children',
},
},
]);

const desiredResult = document[0].children;

这可行,但有没有办法在聚合管道中完全做到这一点?基本上我只想获得“children”数组,没有别的。

EDIT2:上面查询的结果是这个数组和一个对象,这个对象有一个对象数组

[
{
"_id": "6051cacf8f974b3104715ec4",
"parent": null,
"products": [
"60515150d6ac5e08a4e796b4",
"6051b8b0f8c80835480b28d3"
],
"isParent": true,
"name": "Electronics",
"description": "All kinds of devices",
"createdAt": "2021-03-17T09:24:31.365Z",
"updatedAt": "2021-03-20T20:42:50.699Z",
"slug": "electronics",
"__v": 4,
"children": [
{
"_id": "6051cb588f974b3104715ec5",
"parent": "6051cacf8f974b3104715ec4",
"products": [],
"isParent": true,
"name": "Cameras",
"description": "description",
"createdAt": "2021-03-17T09:26:48.220Z",
"updatedAt": "2021-03-17T09:29:35.608Z",
"slug": "cameras",
"__v": 0
},
{
"_id": "6051cb5f8f974b3104715ec6",
"parent": "6051cacf8f974b3104715ec4",
"products": [
"60515150d6ac5e08a4e796b4",
"6051b8b0f8c80835480b28d3"
],
"isParent": true,
"name": "Computers",
"description": "description",
"createdAt": "2021-03-17T09:26:55.364Z",
"updatedAt": "2021-03-20T20:42:50.779Z",
"slug": "computers",
"__v": 4
},
{
"_id": "6051cb6f8f974b3104715ec7",
"parent": "6051cacf8f974b3104715ec4",
"products": [],
"isParent": false,
"name": "Car Electronics",
"description": "description",
"createdAt": "2021-03-17T09:27:11.108Z",
"updatedAt": "2021-03-17T09:27:11.108Z",
"slug": "car-electronics",
"__v": 0
},
{
"_id": "6051cb768f974b3104715ec8",
"parent": "6051cacf8f974b3104715ec4",
"products": [],
"isParent": false,
"name": "TV",
"description": "description",
"createdAt": "2021-03-17T09:27:18.422Z",
"updatedAt": "2021-03-17T09:27:18.422Z",
"slug": "tv",
"__v": 0
}
]
}
]

我想要得到的是那个对象里面的整个“children”数组

[
{
"_id": "6051cb588f974b3104715ec5",
"parent": "6051cacf8f974b3104715ec4",
"products": [],
"isParent": true,
"name": "Cameras",
"description": "description",
"createdAt": "2021-03-17T09:26:48.220Z",
"updatedAt": "2021-03-17T09:29:35.608Z",
"slug": "cameras",
"__v": 0
},
{
"_id": "6051cb5f8f974b3104715ec6",
"parent": "6051cacf8f974b3104715ec4",
"products": [
"60515150d6ac5e08a4e796b4",
"6051b8b0f8c80835480b28d3"
],
"isParent": true,
"name": "Computers",
"description": "description",
"createdAt": "2021-03-17T09:26:55.364Z",
"updatedAt": "2021-03-20T20:42:50.779Z",
"slug": "computers",
"__v": 4
},
{
"_id": "6051cb6f8f974b3104715ec7",
"parent": "6051cacf8f974b3104715ec4",
"products": [],
"isParent": false,
"name": "Car Electronics",
"description": "description",
"createdAt": "2021-03-17T09:27:11.108Z",
"updatedAt": "2021-03-17T09:27:11.108Z",
"slug": "car-electronics",
"__v": 0
},
{
"_id": "6051cb768f974b3104715ec8",
"parent": "6051cacf8f974b3104715ec4",
"products": [],
"isParent": false,
"name": "TV",
"description": "description",
"createdAt": "2021-03-17T09:27:18.422Z",
"updatedAt": "2021-03-17T09:27:18.422Z",
"slug": "tv",
"__v": 0
}
]

正如我提到的,这可以通过 const desiredResult = document[0].children; 来完成,但我想在查询本身中执行此操作。

EDIT3:似乎聚合总是会返回一组文档,即使只有一个文档,我可以选择文档的外观,但是我不能只返回一个字段本身,没有文档。这对我正在尝试做的事情来说很好 - 我可以将该文档(或其特定字段)分配给一个变量。

最佳答案

使用https://mongoosejs.com/docs/populate.html

MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections.

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). We may populate a single document, multiple documents, a plain object, multiple plain objects, or all objects returned from a query. Let's look at some examples.


获取完整数组

{ $project: { _id:0, children: 1 } }

用它来获取第一个元素

从聚合项目管道中的数组中提取第一个元素

{ $project: { _id:0, children: { $arrayElemAt: [ "$children", 0 ] } } }


if(document && document[0] && document[0].children) { // your logic }

$arrayElemAt

$project

组合查询

const document = await Category.aggregate([
{
$match: {
slug: categorySlug,
},
},
{
$lookup: {
from: 'categories',
localField: '_id',
foreignField: 'parent',
as: 'children',
},
},
{ $project: { _id:0, children: { $arrayElemAt: [ "$children", 0 ] } } }
]);

关于mongodb - findOne 然后 findMany 使用在 findOne 中找到的文档中的值,在单个查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66727802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com