使用 GridFS 的 C# MongoDB 驱动程序

发布于 2024-10-31 01:44:31 字数 342 浏览 0 评论 0原文

谁能告诉我存储与单个 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 技术交流群。

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

发布评论

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

评论(3

谁人与我共长歌 2024-11-07 01:44:31

我将在用户文档中存储一组 DBRef 来创建更强的关联。除了图像的唯一 ID 之外,这还为关联(即目标集合)提供了更多上下文。

至于将 ID 存储在图像中,我会跳过它,原因如下:

  • 您不想不断维护双重引用。虽然由于文档数据库的脱节性质,有时这是必要的,但从反向查找中很容易推断出关联。
  • 对用户对象中的引用数组进行索引将通过简单的反向查找从图像中获取用户信息。这是一个简单的查询,所产生的开销可以忽略不计(如果有的话)。

如果需求发生变化以允许单个图像代表多个用户,仅具有单向松散关联有助于确保您的实施面向未来。

I would store an array of DBRefs 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:

  • You don't want to have to constantly maintain the dual references. While this is sometimes a necessity due to the disjointed nature of document databases, the association is easy to infer from the reverse lookup.
  • Indexing the array of references in the user object will get you the user info from an image with a simple reverse-lookup. This is a trivial query and will result in negligible overhead (if any).

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.

热血少△年 2024-11-07 01:44:31
using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using System.IO;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            MongoServer ms = MongoServer.Create();
            string _dbName = "docs";

            MongoDatabase md = ms.GetDatabase(_dbName);
            if (!md.CollectionExists(_dbName))
            {
                md.CreateCollection(_dbName);
            }

            MongoCollection<Doc> _documents = md.GetCollection<Doc>(_dbName);
            _documents.RemoveAll();
            //add file to GridFS

            MongoGridFS gfs = new MongoGridFS(md);
            MongoGridFSFileInfo gfsi = gfs.Upload(@"c:\mongodb.rtf");
            _documents.Insert(new Doc()
            {
                DocId = gfsi.Id.AsObjectId,
                DocName = @"c:\foo.rtf"
            }
            );

            foreach (Doc item in _documents.FindAll())
            {

                ObjectId _documentid = new ObjectId(item.DocId.ToString());
                MongoGridFSFileInfo _fileInfo = md.GridFS.FindOne(Query.EQ("_id", _documentid));
                gfs.Download(item.DocName, _fileInfo);
                Console.WriteLine("Downloaded {0}", item.DocName);
                Console.WriteLine("DocName {0} dowloaded", item.DocName);

            }



            Console.ReadKey();
        }
    }

    class Doc
    {
        public ObjectId Id { get; set; }
        public string DocName { get; set; }
        public ObjectId DocId { get; set; }


    }


}
using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using System.IO;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            MongoServer ms = MongoServer.Create();
            string _dbName = "docs";

            MongoDatabase md = ms.GetDatabase(_dbName);
            if (!md.CollectionExists(_dbName))
            {
                md.CreateCollection(_dbName);
            }

            MongoCollection<Doc> _documents = md.GetCollection<Doc>(_dbName);
            _documents.RemoveAll();
            //add file to GridFS

            MongoGridFS gfs = new MongoGridFS(md);
            MongoGridFSFileInfo gfsi = gfs.Upload(@"c:\mongodb.rtf");
            _documents.Insert(new Doc()
            {
                DocId = gfsi.Id.AsObjectId,
                DocName = @"c:\foo.rtf"
            }
            );

            foreach (Doc item in _documents.FindAll())
            {

                ObjectId _documentid = new ObjectId(item.DocId.ToString());
                MongoGridFSFileInfo _fileInfo = md.GridFS.FindOne(Query.EQ("_id", _documentid));
                gfs.Download(item.DocName, _fileInfo);
                Console.WriteLine("Downloaded {0}", item.DocName);
                Console.WriteLine("DocName {0} dowloaded", item.DocName);

            }



            Console.ReadKey();
        }
    }

    class Doc
    {
        public ObjectId Id { get; set; }
        public string DocName { get; set; }
        public ObjectId DocId { get; set; }


    }


}
乖乖 2024-11-07 01:44:31

我同意@Jake,不需要在图像中存储 userId。

我不仅喜欢在用户文档中存储图像集合 ObjectId,还喜欢存储更多信息(例如文件名、mb 一些元数据)。

使用这种方法,如果您想显示有关用户下载的信息(文件名、文件链接),则无需通过 ids 加载文件。

所以我建议这样的架构:

User { _id, 
       .., 
       Images {
               ImageId, 
               ImageName, 
               .. }
     }

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:

User { _id, 
       .., 
       Images {
               ImageId, 
               ImageName, 
               .. }
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文