[英].net core application needs to talk to mongodb existing collection with _id and id
I've got a question.我有个问题。 I have an existing mongodb collection I need to talk to containing an _id objectId and a UUID saved as string id field.
我有一个现有的 mongodb 集合,我需要与之交谈,其中包含一个 _id objectId 和一个保存为字符串 id 字段的 UUID。
I need to fetch the documents based on a list of uuids -> which are basically the uuids in the id field of the document.我需要根据 uuid 列表获取文档 -> 基本上是文档 id 字段中的 uuid。
I've made a gateway to do this.我已经做了一个网关来做到这一点。 (don't mind the TryAsync thingy that's basically a monad wrapped around a task)
(不要介意 TryAsync 基本上是一个包裹在任务周围的单子)
public TryAsync<IEnumerable<T>> ByIds(List<Guid> ids)
{
var filter = Builders<T>.Filter.In("id", ids.Select(id => id.ToString()));
return TryAsync<IEnumerable<T>>.Apply(() => new List<T>()).SetAsync(() => Collection.Find(filter).ToListAsync());
}
The T is basically: T基本上是:
public interface IDomainObject
{
string id { get; set; }
}
When I run the code I get no result back.当我运行代码时,我没有得到任何结果。
So I decided to do some more testing, I've added the _id as well and tried to fetch it based on a fix _id.所以我决定做更多的测试,我也添加了_id,并尝试根据修复_id来获取它。
Then I get this message => MongoDB.Bson.BsonSerializationException: 'The property 'Id' of type 'RG.Product.Core.Process.Product' cannot use element name '_id' because it is already being used by property '_id''然后我收到此消息=> MongoDB.Bson.BsonSerializationException:'RG.Product.Core.Process.Product' 类型的属性'Id' 不能使用元素名称'_id',因为它已被属性'_id'使用'
Seems like you can't have an _id and id on the same object?好像你不能在同一个对象上有一个 _id 和 id ?
I'm not interested in the _id object id, I just want to fetch the document without the _id field based on the id.我对 _id 对象 id 不感兴趣,我只想根据 id 获取没有 _id 字段的文档。 Hopefully somebody can point me in the right direction?
希望有人能指出我正确的方向吗?
Have a look at the attributes you can use in MongoDB.查看可以在 MongoDB 中使用的属性。
public class DBInfo
{
[BsonId] // With this attribute you can define the _id field name
public ObjectId recordId;
[BsonIgnore]
public DBInfo child; // Ignore this variable
[BsonElement("sp")] // Rename this variable
public string searchPolicy;
[BsonExtraElements] // I always add this attribute on object with Id, this avoid deserialization exception in case of schema change
// All element the deserialization can't match will be added here
public BsonDocument catchAll;
}
https://github.com/iso8859/learn-mongodb-by-example/blob/main/dotnet/01%20-%20Begin/04%20-%20Attributes.cs https://github.com/iso8859/learn-mongodb-by-example/blob/main/dotnet/01%20-%20Begin/04%20-%20Attributes.cs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.