使用 GridFS 的 C# MongoDB 驱动程序
谁能告诉我存储与单个 mongo 文档相关的多个图像的最佳方法?
我的用例是“个人资料”集合中有一个“用户”文档。该文档包含有关用户的元数据(电子邮件地址、电话号码..)。
然后我们就可以上传有关用户的多个图像。
我们使用 GridFS 将图像存储在文件集合中,我从 GridFS 获取上传图像的 ID。
将该图像对象与上传它的用户对象相关联的最佳方法是什么?我可以将用户的 ObjectID 放入图像对象的元数据中……或者我可以将 ObjectID 放入用户文档中(并创建图像集合)。
我们正在使用 C# 驱动程序……所以如果有人有我的用例的任何示例……那就太好了。
谢谢 梅杰德
Can anyone tell me the best way to store multiple images related to a single mongo document?
My use-case is that I have a “user” doc in the “profile” collection. This document has metadata about the user(email address, phone #..).
Then we have the ability to upload multiple images about a user.
We are using GridFS to store the images in the files collection, and I get back the id of the uploaded image from GridFS.
What is the best way to relate that image object back to the user object that uploaded it? I can put the User’s ObjectID in the metadata of the Image Object….or I can put the ObjectID in the User Doc(and create a collection of Images).
We are using the C# driver…so if anyone has any examples of my use-case…that would be great.
Thanks
MJD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将在用户文档中存储一组
DBRef
来创建更强的关联。除了图像的唯一 ID 之外,这还为关联(即目标集合)提供了更多上下文。至于将 ID 存储在图像中,我会跳过它,原因如下:
如果需求发生变化以允许单个图像代表多个用户,仅具有单向松散关联有助于确保您的实施面向未来。
I would store an array of
DBRef
s in the user's document to create a stronger association. This affords more context to the association (i.e., the target collection) in addition to just the unique ID of the image.As for storing the ID in the image, I would skip it for a few reasons:
Having only the one-way loose association helps to future-proof your implementation should the requirements ever change to allow a single image to represent multiple users.
我同意@Jake,不需要在图像中存储 userId。
我不仅喜欢在用户文档中存储图像集合
ObjectId
,还喜欢存储更多信息(例如文件名、mb 一些元数据)。使用这种方法,如果您想显示有关用户下载的信息(文件名、文件链接),则无需通过 ids 加载文件。
所以我建议这样的架构:
I am agree with @Jake, that no need to store userId in image.
I like to store not only collection of images
ObjectId
in the user document, but also some more information (like file name, mb some metadata).With such approch you no need load files by ids if you want display information about user downloads (file name, link to the file).
So i suggest schema like this: