- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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 }
组合查询
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/
我有一个简单的 node.js 代码,它使用 mongoose,它在保存时工作但不检索。 .save() 有效,但 .findOne() 无效。 mongoose = require('mongoos
const categorySlug = req.query.category; const category = await Category.findOne({ slug: categorySlu
我正在使用 Waterline通过 Sails 查询 MySQL 数据库。我找到了 2 种方法。 不知道哪个更好? 顺便问一下,如何处理这两种情况的错误? 1. Model.findOne().whe
我可以在没有数据匹配的情况下将 findOne 查询结果从 NULL 更改为空对象吗?如果是,如何? 先感谢您 最佳答案 我认为此选项的 Sequelize 中没有插入配置。 您可以为模型访问层创建一
我将 NestJS 框架与 TypeORM 和 PostgreSQL 结合使用。假设我有一个简单的 User 实体,它只有两个属性:id 和 name @Entity() class User {
我将 NestJS 框架与 TypeORM 和 PostgreSQL 结合使用。假设我有一个简单的 User 实体,它只有两个属性:id 和 name @Entity() class User {
I'm practicing by following a React, MUI, Nodejs, CRUD project. Currently am able to get the regi
我正在尝试使用 Sequelize 和 mysql db 创建一个模型。我正在尝试发布到“/students/register”,它一直给我一个错误,说 findOne 不是一个函数。我尝试要求我的
有没有可能做findOne在mongodb中解释? 目前我已经尝试过: db.coll.findOne({query}).explain() db.collection.findOne({query}
在我的应用程序中,我尝试使用 .findOne 但出现错误。这是我的错误:- TypeError: User.findOne is not a function at Strategy._verify
我有一个 user.js 文件,用于我网站中的用户模型。这是用户架构: var UserSchema = mongoose.Schema({ username: { type:
我正在从 SQL 迁移到 MonboDB,并且我正在尝试学习基础知识。但到目前为止,我一直在努力解决一个“Select 语句” 假设我有这样的收藏 文档1 { "_id" : "id1", "
login: function (req, res) { var username = req.param('username'); var password = re
潜在客户和客户都是集合。为什么 Leads.findOne() 返回一个对象?它应该像 Customers.findOne() 那样返回一个 Document。 我使用的是 Meteor 1.2.1。
我的 mongodb 集合中的 ids 格式如下 "_id" : NumberLong("502434721406525440"), 我正在尝试执行 mongo 的 findOne 方法来检索具有
我专门使用 mongoose,尽管我认为这没有那么重要。例如,假设我有一个名为 MongoQueue 的集合,我向该队列添加了一些人。 `MongoQueue.save {function(err,
我是第一次使用 Mongodb,我遇到了一个非常奇怪的错误。我有一个“游戏”集合,但我无法使用 _id 查询对其进行搜索。 我直接在 mongo shell 上尝试,结果如下: > db.games.
我的问题是:有一组用户。我试图找到 _id=xxx 的用户是否有 somevalue > 5。 我想知道,使用 find(...).count() > 0 或 findOne(...) != null
我在使用 TypeORM 执行嵌套查找查询时遇到一些问题。这是基本代码: const { completionId } = req?.params; const user = req.
我尝试使用护照添加 Facebook 登录信息,但遇到以下错误: 类型错误:无法在 Strategy._verify 中调用未定义的方法“findOne” 我的 app.js 文件中需要 models
我是一名优秀的程序员,十分优秀!