var dirSchema = new mongoose.Schema([{type:String,
name:String, path:String, children:[dirSchema]}]);
我想在 mongodb 中使用 nodejs 存储这种类型的模式?它就像一个文件系统结构。如果我的路径是/foo/bar/hello/123,我该如何查询集合。
最佳答案
我不记得当时(2014 年)我是如何解决它的,但可以通过使用图查找操作来解决。深度场可用于识别文件夹的深度。
db.dirs.aggregate([
{ $match: {
dirId: "foo"
} },
{$graphLookup:{
from:"product",
startWith:"$_id",
connectFromField:"_id",
connectToField:"parentId",
"depthField": "depth"
as:"children"
}}])
关于javascript - Mongoose 模式内的相同模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26167595/