简体   繁体   English

带有GridFS的C#MongoDB驱动程序

[英]C# MongoDB Driver with GridFS

Can anyone tell me the best way to store multiple images related to a single mongo document? 谁能告诉我最好的方法来存储与单个mongo文档相关的多个图像?

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. 我们正在使用GridFS将图像存储在文件集合中,我从GridFS中获取了上载图像的ID。

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). 我可以将用户的ObjectID放在图像对象的元数据中……,也可以将ObjectID放在用户文档中(并创建图像的集合)。

We are using the C# driver…so if anyone has any examples of my use-case…that would be great. 我们正在使用C#驱动程序……因此,如果有人有我用例的任何示例……那就太好了。

Thanks MJD 谢谢MJD

I would store an array of DBRef s in the user's document to create a stronger association. 我将在用户文档中存储DBRef数组,以创建更强的关联。 This affords more context to the association (ie, the target collection) in addition to just the unique ID of the image. 除了图像的唯一ID外,这还为关联提供了更多上下文(即目标集合)。

As for storing the ID in the image, I would skip it for a few reasons: 至于将ID存储在图像中,出于以下几个原因,我将其跳过:

  • 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. 如果需求发生变化以允许一个图像代表多个用户,则只有单向松散关联有助于您对实现进行过时验证。

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; }


    }


}

I am agree with @Jake, that no need to store userId in image. 我同意@Jake,不需要在图像中存储userId。

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). 我不仅希望在用户文档中存储图像ObjectId集合,而且还希望存储更多信息(例如文件名,mb和一些元数据)。

With such approch you no need load files by ids if you want display information about user downloads (file name, link to the file). 使用这种方法,如果您想显示有关用户下载的信息(文件名,文件链接),则无需按id加载文件。

So i suggest schema like this: 所以我建议这样的模式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM