简体   繁体   中英

Is it possible to add custom fields to gridfs using mongodb C# driver

Can I add my fields to gridfs using C#. For example I have company collection :

[
 { 
  companyid:
...
 },
] 

I want to store my companyid in gridfs file chunks or file reference I don't want to use filename field.

{
    "_id" : ObjectId("53956a7d5f26f52e0c19c5b9"),
    "filename" : "f123db3d16a2d1417c1b67e133373549a729.xsl",
    "length" : NumberLong(15525),
    "chunkSize" : 262144,
    "uploadDate" : ISODate("2014-06-09T08:04:13.093Z"),
    "md5" : "3c47805c7225764cdb15826dfc8c42cb",
    "CompanyId":""(my company collection id)
}

Is this possible?

Yes. it is possible. You can store data in gridfs metadata.

For example:

{
    "_id" : ObjectId("53956a7d5f26f52e0c19c5b9"),
    "filename" : "f123db3d16a2d1417c1b67e133373549a729.xsl",
    "length" : NumberLong(15525),
    "chunkSize" : 262144,
    "uploadDate" : ISODate("2014-06-09T08:04:13.093Z"),
    "md5" : "3c47805c7225764cdb15826dfc8c42cb",
    metadata: null
}

You should be able to add it to metadata just fine. Just make sure you add an index on the metadata field that you want to query on:

db.colName.ensureIndex( { 'metadata.CompanyId' : 1 } );

I hope that supports your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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