在 MongoDB doc 中存储辅助文档
假设 User2 有这些数据对象,它们作为对象数组存储在 User2.objects 中。
User2:{objects:[{object1},{object2},{object3}]}
如果除 User2 之外的任何人都需要查询此数据,就像 User1 需要与他们相关的任何对象一样。那么它应该在 MongoDB 数据库中分解成它自己的集合,对吧?
因为假设 User1 想要找到每个类似的对象。他们需要引用他们创建的所有 User2 数据,然后查看每个用户中的每个对象,并返回他们需要的对象。
我应该将这些对象分成它们自己的集合吗?然后我可以只索引用户 id,每个用户只需查询一次他们自己的 id。
抱歉,如果这个问题令人困惑,我有点迷失了。
Say User2 has these objects of data, they are stored in User2.objects as an array of objects.
User2:{objects:[{object1},{object2},{object3}]}
If anyone other then User2 needs to query this data, like User1 needs any of those objects that pertain to them. Then it should be broken out into it's own collections of in MongoDB db right?
Because say a User1 wants to find every object like that are part of. They would need to have a reference to all the User2s they created data with then look through every object in each user, and return the one they need.
I should break those objects out into their own collections? Then I can just index user ids and each user can just query once for their own id.
Sorry if this Q is confusing I'm a little lost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来 Mongo Document 结构和使用 MongoDB 进行身份验证之间可能存在一些混淆。
有关如何为 Mongo 数据库设置用户身份验证的文档位于:
http://www.mongodb.org/display/DOCS/Security+and+Authentication
如果用户 2 需要对用户 1 创建的集合运行查询,然后用户 2 必须拥有该集合所在数据库的帐户,并且必须经过正确的身份验证。
提供的示例文档也有点令人困惑。最好使用所有文档中都相同的键名称。例如:
优于
在第二个示例中,必须知道用户名(user1、user2 等)才能找出用户的名称。 MongoDB 不支持查询中的通配符。
以下文档结构更可取:
可以使用以下查询检索 user1 创建的所有对象:
有关 MongoDB 文档结构的更多信息,我建议阅读以下内容:
http://www.mongodb.org/display/DOCS/Schema+Design - 对 MongoDB 中数据存储方式的精彩介绍,包括示例文档,最佳实践以及索引简介。
希望以上内容能让您在决定集合架构、创建用户和设置权限方面走上正确的道路。身份验证是 MongoDB 更高级的功能之一,因此在担心身份验证之前,我首先会重点关注构建高效的模式并正确组织数据。
如果您对这些主题或任何其他 MongoDB 相关问题有任何其他问题,社区随时为您提供帮助!祝你好运!
It appears as though there may be some confusion between Mongo Document structure and using authentication with MongoDB.
The documentation on how to set up user authentication for a Mongo Database is here:
http://www.mongodb.org/display/DOCS/Security+and+Authentication
If User2 needs to run a query on a collection that was created by User1, then User2 must have an account with the Database where that collection resides, and must be properly authenticated.
The example document provided is also a little confusing. It is a better idea to use key names that will be the same across all documents. For example:
is preferable to
In the second example, the username (user1, user2, etc) will have to be known in order to find out the name of the user. MongoDB does not support wildcards in queries.
The following document structure would be preferable:
All of the objects created by user1 could be retrieved with the following query:
For more information on the MongoDB document structure, I recommend reading the following:
http://www.mongodb.org/display/DOCS/Schema+Design - A great introduction to the way data is stored in MongoDB, including example documents, best practices, and an introduction to indexing.
Hopefully the above will put you on the right track in terms of deciding on a schema for your collection and creating users and setting permissions. Authentication is one of MongoDB's more advanced features, so I would begin by focusing on building an efficient schema and organizing your data correctly before worrying about authentication.
If you have any additional questions about these topics, or anything else MongoDB-related, the Community is here to help! Good Luck!