通过MongoDB中的中间儿童收集找到父对象

发布于 2025-02-12 18:53:26 字数 1480 浏览 1 评论 0原文

在我的Mongo数据库中,我有一个项目列表,并且在任何给定项目中都有不同权限的项目列表。任何项目都可能有多个用户,任何项目用户可能对多个项目都有权限。我已经将项目用途和项目定义为实体,并将其作为项目的子记录。

我的模式的相关部分看起来像这样:

const ProjectUserSchema = {
    name: { type: String, required: true },
    email: { type: String, required: true }
    // and so on.
};

const UserPermissionSchema = {
    user: { type: Schema.Types.ObjectId, ref: "ProjectUser" },
    level: { type: Number, required: true }
}

const ProjectSchema = {
    name: { type: String, required: true },
    permissions: [ UserPermissionSchema ]
}

这对于与项目进行互动效果很好,因为我们可以快速告诉用户拥有哪些权限,但是我无法弄清楚我需要做什么以查询所有项目当前用户已启用权限。

示例项目对象看起来像这样:

{ "_id" : ObjectId("62b44bc0aaa95c60c065df60"), 
 "name" : "My super-secret plan",
  "permissions" : [ 
      { "user" : ObjectId("62b9d3691af5d2cb26e08574"), "level" : 1,  "_id" : ObjectId("62bed68c4d58c0c32c66c571") }, 
      { "user" : ObjectId("62befc5429b845a8eb552423"), "level" : 1, "_id" : ObjectId("62bf041629b845a8eb5524b5") } 
   ], 
   "__v" : 13 
} 

我尝试了一个简单的db.projects.find(“ permissions.user._id”:objectid(“ 62b9d3691af5d2cb2cb26e08574”)) > db.projects.find({“ permissions”:{$ elemmatch:{ “ field”:“用户”,“ value”:ObjectID(“ 62BEFC5429B845A8EB5552423”)}}}}})});,但周围的这些值的调整似乎没有起作用。

我正在为我的应用程序代码使用Mongoose,我假设在命令行中的查询工作之前,没有太多要点了,但是如果Mongoose提供任何有助于解决此问题的实用程序方法,这些方法将提供非常非常可接受的答案。

我觉得这应该是一个相当标准的案例,但是我找不到明显的解决方案来解决我的问题,或者我不知道要搜索的正确的mongo术语。如何根据子记录链接关系查询父对象?

In my Mongo database I have a list of Projects and a list of ProjectUsers any of whom potentially have different permissions on any given Project. Any Project may have multiple users and any ProjectUser may have permissions on multiple Projects. I have defined the ProjectUser and the Project as entities, with a Permissions collection as a subdocument of the Project.

The relevant parts of my schema look like this:

const ProjectUserSchema = {
    name: { type: String, required: true },
    email: { type: String, required: true }
    // and so on.
};

const UserPermissionSchema = {
    user: { type: Schema.Types.ObjectId, ref: "ProjectUser" },
    level: { type: Number, required: true }
}

const ProjectSchema = {
    name: { type: String, required: true },
    permissions: [ UserPermissionSchema ]
}

This works fine for interacting with a Project as we can quickly tell what users have which permissions, but I can't figure out what I need to do in order to query all the projects that the current user has permissions on.

A sample Project object looks like this:

{ "_id" : ObjectId("62b44bc0aaa95c60c065df60"), 
 "name" : "My super-secret plan",
  "permissions" : [ 
      { "user" : ObjectId("62b9d3691af5d2cb26e08574"), "level" : 1,  "_id" : ObjectId("62bed68c4d58c0c32c66c571") }, 
      { "user" : ObjectId("62befc5429b845a8eb552423"), "level" : 1, "_id" : ObjectId("62bf041629b845a8eb5524b5") } 
   ], 
   "__v" : 13 
} 

I have tried a simple db.projects.find("permissions.user._id": ObjectId("62b9d3691af5d2cb26e08574")) and more complicated queries using aggregations like db.projects.find({ "permissions": { $elemMatch: { "field": "user", "value": ObjectId("62befc5429b845a8eb552423")}}}); but no amount of tweaking those values around seems to be working.

I'm using Mongoose for my application code, I assume that until I can get the query working in the command line there's not much point going any further but if Mongoose provides any utility methods that would help solve this problem, those would provide a very acceptable answer.

I feel like this should be a fairly standard case, but I can't find an obvious solution for my problem, or possibly I don't know the correct Mongo terminology to search for. How do I query a parent object based on a subdocument linked relationship?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2025-02-19 18:53:27

权限阵列中的用户是一个objectid,它指用户集合中的文档。那里没有_id。您的查询应该是:

db.projects.find({"permissions.user": ObjectId("62b9d3691af5d2cb26e08574")})

User in the permissions array is an ObjectID which refers to the document in users collection. There is no _id there. Your query should be:

db.projects.find({"permissions.user": ObjectId("62b9d3691af5d2cb26e08574")})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文