2 回答
TA贡献1825条经验 获得超4个赞
使用MongoDb.Driver 包的基本示例。您需要定义一些数据类型,例如:
// Use [BsonIgnoreExtraElements] attribute when not defining ALL fields in record
internal class MainRecord
{
public ObjectId _id { get; set; }
public List<ArrayItem> ResourceStatus { get; set; }
// All the other fields here
}
// [BsonIgnoreExtraElements] as above
internal class ArrayItem
{
public string E2EId { get; set; }
}
(注意——我省略了数组搜索不需要的所有字段)。
然后实际查询数据:
var client = new MongoClient();
IMongoDatabase db = client.GetDatabase("database-name-here");
var collectionName = "collection-name-here";
IMongoCollection<MainRecord> collection = db.GetCollection<MainRecord>(collectionName);
var filter = Builders<MainRecord>.Filter.ElemMatch(x => x.ResourceStatus, x => x.E2EId == "1fdsfsfsfsfsffds0");
var result = collection.Find(filter);
编辑:我建议查看这篇文章,它提供了一些替代方法。
TA贡献1805条经验 获得超9个赞
我创建简单的类来演示如何:
MongoDB 类
public class MongoDBConnect : IDisposable
{
public IMongoClient client;
public IMongoDatabase database;
public MongoDBConnect()
{
client = new MongoClient("mongodb://localhost");
database = client.GetDatabase("dbo");
}
public void Dispose()
{
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
你的收藏课
public class YourCollection
{
[BsonId()]
public ObjectId Id { get; set; }
[BsonElement("YourCollectionID")]
public string YourCollectionID { get; set; }
[BsonElement("AccessKey")]
public string AccessKey { get; set; }
}
您的集合数据类
public class YourCollectionDAO : MongoDBConnect
{
public YourCollectionDAO()
{
}
public YourCollection Find(string yourCollectionID)
{
var collection = this.database.GetCollection<User>("YourCollection");
Expression<Func<YourCollection, bool>> filter = x => x.yourCollectionID == yourCollectionID;
IList<YourCollection> filtering = collection.Find(filter).ToList();
var yourCollectionItem = filtering.Where(x => x.yourCollectionID == yourCollectionID).FirstOrDefault();
return yourCollectionItem;
}
}
希望能帮助到你。
- 2 回答
- 0 关注
- 109 浏览
添加回答
举报