public static void Test_LoadMongoDetail_01() { Trace.WriteLine("Test_LoadMongoDetail_01"); Trace.WriteLine(); RapideDdl.InitMongoClassMap(); if (BsonClassMap.IsClassMapRegistered(typeof(RapideDdl_Base))) { BsonClassMap map = BsonClassMap.LookupClassMap(typeof(RapideDdl_Base)); Trace.WriteLine("change existing class map"); BsonMemberMap memberMap = map.GetMemberMap("infos"); memberMap.SetSerializationOptions(DictionarySerializationOptions.ArrayOfDocuments); } else { Trace.WriteLine("register class map"); BsonClassMap.RegisterClassMap <RapideDdl_Base>(cm => { cm.AutoMap(); cm.GetMemberMap(c => c.infos).SetSerializationOptions(DictionarySerializationOptions.ArrayOfDocuments); }); } Trace.WriteLine(); string query = "{ _id: 35105 }"; MongoCursor <BsonDocument> cursor = MongoCommand.GetDatabase(null, "dl").GetCollection("RapideDdl_Detail").zFind <BsonDocument>(new QueryDocument(BsonSerializer.Deserialize <BsonDocument>(query))); int i = 1; foreach (BsonDocument document in cursor) { BsonDocument document2 = (BsonDocument)document["download"]; RapideDdl_PostDetail postDetail = BsonSerializer.Deserialize <RapideDdl_PostDetail>(document2); //MongoDB.Bson.Serialization.IBsonSerializationOptions options; //MongoDB.Bson.Serialization.BsonSerializer.Serialize() //Serialization.Options //SerializationOptions //MongoDB.Bson.Serialization.Options.RepresentationSerializationOptions //document2 = postDetail.ToBsonDocument(new DictionarySerializationOptions(DictionaryRepresentation.ArrayOfDocuments)); //DictionarySerializationOptions. //BsonClassMap.RegisterClassMap<RapideDdl_PostDetail>(cm => { cm.MapProperty(c => c.SomeProperty); cm.MapProperty(c => c.AnotherProperty); }); //document2 = postDetail.ToBsonDocument(); Trace.WriteLine("document no {0}", i++); //DocumentSerializationOptions options = new DocumentSerializationOptions(); //Trace.WriteLine(postDetail.ToJson(new DictionarySerializationOptions(DictionaryRepresentation.ArrayOfDocuments))); //RapideDdl_PostDetail RapideDdl_Base //BsonClassMap<RapideDdl_Base> map = BsonClassMap.LookupClassMap(typeof(RapideDdl_Base)); //Trace.WriteLine(document.zToJson()); Trace.WriteLine(postDetail.zToJson()); Trace.WriteLine(); } }
public static BsonDocument ReadMongoDocument(string database, string collectionName, object key, string itemName, string server = null) { MongoDatabase mdb = MongoCommand.GetDatabase(server, database); Trace.Write("read mongo document key \"{0}\" from collection \"{1}\" ", key, collectionName); Trace.WriteLine("server \"mongodb://{0}\" database \"{1}\"", mdb.Server.Instance.Address, mdb.Name); MongoCollection collection = mdb.GetCollection(collectionName); BsonDocument document = collection.FindOneByIdAs <BsonDocument>(BsonValue.Create(key)); if (document == null || !document.Contains(itemName)) { Trace.WriteLine("document is null or does'nt contain element \"{0}\"", itemName); return(null); } var element = document[itemName]; if (element == null || !(element is BsonDocument)) { Trace.WriteLine("element \"{0}\" is null or is'nt a BsonDocument", itemName); return(null); } document = element as BsonDocument; return(document); }
public static void Test_RapideDdl_Images_01() { //pb.Data.Mongo.MongoCommand.FindAs("dl", "RapideDdl_Detail", "{}", fields: "{ 'download.images.Source': 1, 'download.images.ImageWidth': 1, 'download.images.ImageHeight': 1 }", limit: 5); //string query = "{ 'download.images.ImageHeight': { $gt: 100 } }"; string query = "{}"; string fields = "{ 'download.images.Source': 1, 'download.images.ImageWidth': 1, 'download.images.ImageHeight': 1 }"; int limit = 0; MongoCursor <BsonDocument> cursor = MongoCommand.GetDatabase(null, "dl").GetCollection("RapideDdl_Detail").zFind <BsonDocument>(new QueryDocument(BsonSerializer.Deserialize <BsonDocument>(query))); cursor.SetFields(new FieldsWrapper(BsonSerializer.Deserialize <BsonDocument>(fields))); if (limit != 0) { cursor.SetLimit(limit); } //DataTable dt = new DataTable(); //dt.Load() //dt.LoadDataRow() //dt.ReadXml() //foreach (BsonDocument document in cursor) //{ // BsonArray array = document.GetElement("download").Value.AsBsonDocument.GetElement("images").Value.AsBsonArray; // foreach (BsonValue value in array) // { // //Trace.WriteLine("BsonType : {0}", value.BsonType); // Trace.WriteLine(value.ToJson()); // } //} //DataTable dt = Test_Bson.Test_Bson_f.BsonDocumentsToDataTable(cursor); DataTable dt = cursor.zToDataTable2(); //dt.Select(); //dt.Rows[0].Delete //foreach (DataRow row in dt.Rows) //{ // object imageHeightValue = row["download.images.ImageHeight"]; // //if (imageHeight is int && (int)imageHeight <= 60) // // row.Delete(); // int imageHeight; // if (imageHeightValue is string && int.TryParse((string)imageHeightValue, out imageHeight)) // { // if (imageHeight <= 60) // row.Delete(); // } //} for (int i = 0; i < dt.Rows.Count;) { DataRow row = dt.Rows[i]; object imageHeightValue = row["download.images.ImageHeight"]; //if (imageHeight is int && (int)imageHeight <= 60) // row.Delete(); int imageHeight; if (imageHeightValue is string && int.TryParse((string)imageHeightValue, out imageHeight) && imageHeight <= 60) { //if (imageHeight <= 60) row.Delete(); } else { i++; } } RunSource.CurrentRunSource.SetResult(dt); }