[CSHARP-757] Overriding Collection Name in MongoDb Created: 11/Jun/13  Updated: 05/Apr/19  Resolved: 11/Jun/13

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.8.1
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: joseph rodriguez Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 2008, Windows 7



 Description   

I have a .Net object named CacheItem<T> that gets persisted to MongoDb. When the object gets persisted, the name of the collection becomes CacheItem`1. If I try to use and shell commands against that document I get 'javascript illegal' error message due to the name that contains the tick mark, '`'. Is there a way to set the name of the object so that it isnt named as such?



 Comments   
Comment by Craig Wilson [ 11/Jun/13 ]

Joseph,
Yes, you are right. Your GetDocumentCollection<T> method is the one creating the collection name. You'll need to figure out how to change that yourself. One option would be to change any ` marks to an _, or something like that. Alternatively, you could just drop anything starting at the ` mark. However, that would create some collisions between, for instance, Tuple<T> and Tuple<T1, T2>.

Hope that helps. I'm going to close this ticket as works as designed. Feel free to post back if you need some more help or clarification.

Thanks,
Craig

Comment by joseph rodriguez [ 11/Jun/13 ]

Here is my save method:

public void Add<T>(string key, T item, int expiryMinutes = 0)
{
if (_enableCache)
{
key = typeof (T).FullName + key;
var collection = _client.GetDocumentCollection<CacheItem<T>>();
var cacheItem = new CacheItem<T>

{ CacheKey = key, Expires = expiryMinutes == 0 ? DateTime.UtcNow.AddMinutes(_expiryMinutes) : DateTime.UtcNow.AddMinutes(expiryMinutes), Item = item }

;

collection.Save(cacheItem);
}
}

public MongoCollection<T> GetDocumentCollection<T>()

{ return Database.GetCollection<T>(typeof(T).Name); }

So my helper method is using the name of the object. Since the object is of generic type, that is where that name with the tick mark is coming from. I should provide an override to explicitly state the name of the object in this case.

Thanks

Comment by Robert Stam [ 11/Jun/13 ]

The driver does not set the name of the collection. The application code sets the name of the collection, using code something like this:

MongoDatabase database;
var collection = database.GetCollection<CacheItem<T>>("cacheitems");

Can you provide some sample code where you are calling GetCollection?

Generated at Wed Feb 07 21:37:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.