[英]Same Schema within Schema in Mongoose
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"
}}])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.