I'm working with latest C# driver for MongoDB. I know it's beta now, but I think I'm doing some basic things.
What's my problem: I'm trying to set representation for my Id field to ObjectId instead of string like it is described in documentation:
BsonClassMap.RegisterClassMap<Entity>(cm =>
{
cm.AutoMap();
cm.IdMemberMap.SetRepresentation(BsonType.ObjectId);
});
But I can not do that because method SetRepresentation() does not exist. And I can not find anything similar.
So I wonder, was this method removed?
Is there any other way to set representation besides attributes? I can not use attributes because I don't have access to Entity class, I'm working with derived class.
Thanks in advance!
I've spoken with the developer of the driver and he clarified this situation:
We've brought all those options into the serializers themselves, so, in this case, you'll want to set the serializer. IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId)); //It's a string which will be represented as an ObjectId in the database.
this works for me (v2.2)
cm.MapIdMember(c => c.Id)
.SetSerializer(new StringSerializer(BsonType.ObjectId))
.SetIdGenerator(StringObjectIdGenerator.Instance);
it's represent objectId in database (not string)
"_id" : ObjectId("56715ebddb6986202816e566"),
Related
I am creating a web API. I need something like this:
When I updating a document at mongodb, I do not want to update a field (createdAt). I know that I can get a old value of that field and manuelly and then put it updated object but it requires one more unnecessarry request to db. I do not want this. My method is here:
public async Task<bool> UpdateAsync(Customer updatedCustomer)
{
var result = await _mongoService.Customers.ReplaceOneAsync(c => c.Id == updatedCustomer.Id, updatedCustomer);
return result.IsModifiedCountAvailable && result.ModifiedCount>0;
}
Is there any way to exclude one property of my Customer class (createdAt) and left it same everytime. BTW please do not recomend that set all properties update one by one by using "Set" method. Thank you.
I'm not sure if there is a way other than to set the properties one by one, but researching the following may be helpful or suggestive of something new.
In Mongodb you can use some decoration to do like [BsonIgnore] but it will ignore it every time
One alternative would be to load the document you wish to modify, followed by calling BsonDocument.Merge with overwriteExistingElements set to true in order to merge your changes.
I have a C# ASP.NET server that returns Animals objects list to my angular 8 project,
Is it possible to set more fields in the Animal class on the client side- angular?
Hope you understand me,
Thanks for your answer!
Here is an example (assume everything is working at this point):
this.http.get<Animal[]>(....).pipe(
flatMap(items => items),
map(item => ...your mapping function here...)
);
See also more examples from this question
I Use ArangoDB Graph Database with ArangoDB-NET(C# Driver).
how can I map properties of c# object to attributes of ArangoDB?
more than any thing, I need to mapping when I insert a document, because of the ArangoDB's id create according of this pattern : "CollectionName/_key"
ArangoDB's id= _key(attribute) <================> C#' id= Id(property)
any one can help me?
thanks a lot
You can try to use AliasField attribute which should map specified field to property. Dummy entity in unit tests uses this attribute to map lower case version of properties.
In C# I have several classes where I have the following "logic":
//objA and objB are instances of the same class
objA.Property1 = objB.Property1;
objA.Property2 = objB.Property2;
objA.Property3 = objB.Property3;
objA.Property4 = objB.Property4;
.....
objA.Property40 = objB.Property40;
I do not want to apply the value to all the properties (but a big part of them).
This is getting repetitive and if somehow the "class" changes where there is a new property there is no compilation error and if the programmer does not pay attention there will be a bug because we will be losing that property change.....
I was wondering if there is a way to the following:
--> Apply the value of all the properties EXCLUDING the ones that specifically point, something like:
//possible syntax
objA.SetAllPropertiesLess(objB, x => x.Id && x.CreateDate && x.IsDeleted);
Does not need to be so "fancy" like this one, but for starting I would like to know if there is already something similar in the .NET framework, then if not, perhaps someone would already have an implementation (lightweight/fast).
Use AutoMapper.
Install it from nuget.org by following command:
PM> Install-Package AutoMapper
Then import it in class file and use it like below:
CreateMap<objA, objB>().ForDestinationMember(x => x.IsDeleted, opt => opt.Ignore());
Hope this will help you. Let me know if you have any doubt.
You can achieve that using AutoMapper and conditional mapping.
I have this JSON container that has a strange field called "48x48" for a photoUrl.
using Newtonsoft.Json;
(...)
dynamic issuesJson = JsonConvert.DeserializeObject(responseIssues.Content);
foreach (dynamic issue in issuesJson.issues){
Console.WriteLine(issue.name); //works properly
Console.WriteLine(issue.48x48); //error -> expected;
}
For some reason Visual Studio doesn't accept the access to this runtime field of this dynamic object. How can I work around this problem?
Note: I cannot change the field name.
Thanks anyway.
For some reason Visual Studio doesn't accept the access to this runtime field of this dynamic object.
Well what you've provided is simply not valid C#. An identifier can't start with a digit. That's still enforced even when you're trying to resolve a member of dynamic.
We don't know what type you're using for issues, but basically you'll need to handle it as a key/value map which you can access by string. Quite how you do that will depend on the implementation of issue. It doesn't look like Json.NET guarantees anything there - you may be able to cast it to JObject, for example:
foreach (JObject issue in issuesJson.issues) {
Console.WriteLine(issue["48x48"]);
}
Field names cannot start with a number. Sorry, no way around it.
You'll have to consult the documentation of your deserializer to see how it takes care of cases like that. It may be as simple as renaming the field "_48x48".
EDIT: actually, based on your code, you probably don't have a class representing this JSON object; I'm leaving my answer anyway, in case it helps someone else.
As others have mentioned, a C# identifier can't start with a digit. You just need to rename 48x48 to a valid name in your class, and map it to the actual JSON name using the [JsonProperty] attribute:
[JsonProperty("48x48")]
public string _48x48 { get; set; }