【问题标题】:BsonDocument contains an array. Need to access the array elements in C# when the BsonDocument is contained by a C# listBsonDocument 包含一个数组。当 BsonDocument 包含在 C# 列表中时需要访问 C# 中的数组元素
【发布时间】:2014-11-24 07:24:55
【问题描述】:

我有一个 mongodb 集合“用户”,它以以下格式存储文档 -

{
    "_id" : ObjectId("53fe7ae0ef038fee879263d5"),
    "username" : "John"
    "status" : "online",
    "profile" : [ 
        {
            "name" : "John Stuart",
            "age" : "23",
            "gender" : "male"
        }
    ]
}

我在 C#.NET 中使用以下函数将集合中的文档存储到 BsonDocuments 列表中 -

    public static List<BsonDocument> LoadDataByWhere (string table, string whereClause)
    {
        // Here table is the collection name and whereClause is the mongodb query 

        var collection = db.GetCollection (table);
        QueryDocument whereDoc = new QueryDocument(BsonDocument.Parse(whereClause));
        var resultSet = collection.Find (whereDoc);
        List<BsonDocument> docs = resultSet.ToList();

        if (resultSet.Count() > 0) {
            foreach(BsonDocument doc in docs)
            {
                doc.Set("_id", doc.GetElement("_id").ToString().Split('=')[1]);
            }
            return docs;
        } 
        else {
            return null;
        }
    }

假设我将返回的列表存储在列表中。我可以使用 -

    List<string> username = new List<string>();

    foreach(BsonDocument item in list) 
    {
        username.Add(Convert.ToString(list.getElement("username").Value));
    }

但是如何使用类似于上述方法的方法在 C# 中获取名称、年龄和性别等数组元素值?

【问题讨论】:

    标签: c# arrays mongodb


    【解决方案1】:

    我建议将 Mongo 文档映射到某种形式的 DTO。 Mongo 支持反序列化为对象图。

    public class User
    {   
        [BsonId]
        public ObjectId Id { get; set; }
    
        public string username { get; set; }
    
        public string status { get; set; }
    
        public List<Profile> profile { get; set; }
    }
    
    
    public class Profile
    {
    
        public string name { get; set; }
    
        public string age { get; set; }
    
        public string gender { get; set; }
    }
    

    然后你可以像这样访问它

    const string connectionString = "mongodb://localhost";
    
    //// Get a thread-safe client object by using a connection string
    var mongoClient = new MongoClient(connectionString);
    
    //// Get a reference to a server object from the Mongo client object
    var mongoServer = mongoClient.GetServer();
    
    //// Get a reference to the database object
    //// from the Mongo server object
    const string databaseName = "mydatabase";
    var db = mongoServer.GetDatabase(databaseName);
    
    //// Get a reference to the collection object from the Mongo database object
    //// The collection name is the type converted to lowercase + "s"
    MongoCollection<T> mongoCollection = db.GetCollection<T>(typeof(T).Name.ToLower() + "s");
    

    因此,当您现在按 id 查询时,它将包含已填充的配置文件列表(如果存在)

    use-csharp-driver

    mongodb-with-c-deep-dive

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      相关资源
      最近更新 更多