【问题标题】:Get field from a sub object in MongoDB从 MongoDB 中的子对象获取字段
【发布时间】:2021-03-19 01:07:27
【问题描述】:

我在 MongoDB 中有以下文档,我想检查字段 FileName 是否具有特定值。

以下是我的课程:

public class Invoice
{

    private InvoiceMetaData _metadata = null;
    private List<InvoiceColumns> _invoiceFields = null;
    public InvoiceMetaData Metadata
    {
        get
        {
            if (_metadata == null) _metadata = new InvoiceMetaData();
            return _metadata;
        }
        set { _metadata = value; }
    }
    public List<InvoiceColumns> InvoiceFields
    {
        get
        {
            if (_invoiceFields == null)
                _invoiceFields = new List<InvoiceColumns>();
            return _invoiceFields;
        }
        set { _invoiceFields = value; }
    }
}

 public class InvoiceMetaData
    {
        public string FileName { get; set; }
        public string FileProcessedOn { get; set; }
        public string DirectoryPath { get; set; }
    }

我已尝试使用以下内容,但即使存在具有此文件名的文档,它也会返回 false。

string filename = "01.png";
 var collection = myDB.GetCollection<Invoice>(collection_name);
 var exists = collection.AsQueryable().Any(avm => avm.Metadata.FileName == filename);

我也试过这个,但它什么也没返回,即列表计数为 0。

var query = Query<Invoice>.EQ(u => u.Metadata.FileName, filename).ToBsonDocument();
var exist = collection.Find(query).ToList();

也试过了,list Count 是 0,

 var filter1 = Builders<Invoice>.Filter.Eq(u => u.Metadata.FileName, filename, filename);
 var result = collection.Find(filter1).ToList();

谁能告诉我我做错了什么? 任何帮助将不胜感激。

【问题讨论】:

  • 文件名在类 InvoiceMetaData 中,只存在一项:myDB.InvoiceMetaData.Filename
  • 是的,但我正在发票类中创建发票元数据的属性。我不能通过 Builders.Filter.Eq(u => u.Metadata.FileName, filename, filename);
  • 它不是可枚举项(也不是集合)。属性只有一份副本。您应该能够通过:myDB._metadata.FileName;这个对象是你的集合:private List _invoiceFields = null;
  • myDB._metadata.FileName 给出错误“MongoDatabaseBase”不包含“_metadata”的定义并且没有可访问的扩展方法“_metadata”真的很抱歉再次打扰这是我第一次处理这个问题。
  • 数据库的模型和c#中的类不一致。请参阅:medium.com/@hanjchen/…

标签: c# mongodb mongodb-.net-driver


【解决方案1】:

我已经有一段时间没有处理这个问题了。但这可能对您有所帮助:

  • how to check if a field exists in a specific document Mongodb using C#?

  • 我记得,你可以这样做:

    var collection = myDB.GetCollection<BsonDocument>(collection_name); // you use Invoice, try BsonDocument
    var documents = collection.Find(new BsonDocument()).ToList(); // this one should get you all documents
    var fieldName = "Metadata" // name of field you need data from
    
    foreach (var document in documents)
    {  
      // this one should contain your metadata object from document, note it is BsonDocument   
      var metaDataObject= document.Contains(fieldName) ? document[fieldName].ToBsonDocument() : null;        
    
      var fileName= metaDataObject!= null ? metaDataObject["FileName"] : "No file name."; // should be your file name          
    
      // you also should be able to convert to dictionary, under this namespace MongoDB.Bson.BsonDocument
      // var metadataDic= metaDataObject.ToDictionary();
      // var fileName= metadataDic["FileName"]; // should be your file name        
    }      
    
  • 过滤示例:

    var builder = Builders<BsonDocument>.Filter; // in your example invoice, try BsonDocument
    var filter = builder.Eq("FileName", fileName);  
    var count = collection.Count(filter);
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2014-11-12
    相关资源
    最近更新 更多