Beispiel #1
0
        private BsonDocument CreateDoc()
        {
            // create same object, but using BsonDocument
            var doc = new BsonDocument();

            doc.Id             = 123;
            doc["FirstString"] = "BEGIN this string \" has \" \t and this \f \n\r END";
            doc["CustomerId"]  = Guid.NewGuid();
            doc["Date"]        = new DateTime(2015, 1, 1);
            doc["MyNull"]      = null;
            doc["Items"]       = new BsonArray();
            doc["MyObj"]       = new BsonObject();
            doc["EmptyString"] = "";
            var obj = new BsonObject();

            obj["Qtd"]         = 3;
            obj["Description"] = "Big beer package";
            obj["Unit"]        = 1299.995m;
            doc["Items"].AsArray.Add(obj);
            doc["Items"].AsArray.Add("string-one");
            doc["Items"].AsArray.Add(null);
            doc["Items"].AsArray.Add(true);
            doc["Items"].AsArray.Add(DateTime.Now);

            doc["MyObj"]["IsFirstId"] = true;

            return(doc);
        }
Beispiel #2
0
        public static byte[] Serialize(object[] methodParams)
        {
            List <String> complexParams = new List <String>();

            object[] payload = new object[methodParams.Length + 1];

            //elements
            int count = 1;

            foreach (object param in methodParams)
            {
                payload[count] = param;
                complexParams.Add(param.GetType().FullName);
                count++;
            }

            //header type
            payload[0] = complexParams;

            using (MemoryStream memoryStream = new MemoryStream(4096))
            {
                using (BsonWriter bsonWriter = new BsonWriter(memoryStream))
                {
                    BsonObject bson = new BsonObject();
                    bson.Payload = payload;

                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(bsonWriter, bson);
                }
                return(memoryStream.ToArray());
            }
        }
Beispiel #3
0
		public static byte[] Serialize(object[] methodParams)
		{
			List<String> complexParams = new List<String>();
			object[] payload = new object[methodParams.Length + 1];

			//elements
			int count = 1;
			foreach (object param in methodParams)
			{
				payload[count] = param;
				complexParams.Add(param.GetType().FullName);
				count++;
			}

			//header type
			payload[0] = complexParams;

			using (MemoryStream memoryStream = new MemoryStream(4096))
			{
				using (BsonWriter bsonWriter = new BsonWriter(memoryStream))
				{
					BsonObject bson = new BsonObject();
					bson.Payload = payload;

					JsonSerializer serializer = new JsonSerializer();
					serializer.Serialize(bsonWriter, bson);
				}
				return memoryStream.ToArray();
			}
		}
Beispiel #4
0
		public static byte[] Serialize(object objReturn)
		{
			object[] payload = new object[2];

			payload[0] = objReturn.GetType().FullName;
			payload[1] = objReturn;

			using (MemoryStream memoryStream = new MemoryStream(4096))
			{
				using (BsonWriter bsonWriter = new BsonWriter(memoryStream))
				{
					BsonObject bson = new BsonObject();
					bson.Payload = payload;

					JsonSerializer serializer = new JsonSerializer();
					serializer.Serialize(bsonWriter, bson);
				}
				return memoryStream.ToArray();
			}
		}
Beispiel #5
0
        public static byte[] Serialize(object objReturn)
        {
            object[] payload = new object[2];

            payload[0] = objReturn.GetType().FullName;
            payload[1] = objReturn;

            using (MemoryStream memoryStream = new MemoryStream(4096))
            {
                using (BsonWriter bsonWriter = new BsonWriter(memoryStream))
                {
                    BsonObject bson = new BsonObject();
                    bson.Payload = payload;

                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(bsonWriter, bson);
                }
                return(memoryStream.ToArray());
            }
        }
Beispiel #6
0
 /// <summary>
 /// Returns information about the profile level for this database
 /// </summary>
 public ProfileResult(BsonObject document)
     : base(document)
 {
 }
 /// <summary>
 /// Creates the detail for a Database listed in Mongo
 /// </summary>
 public DatabaseDetailResult(BsonObject detail)
     : base(detail)
 {
 }
Beispiel #8
0
 /// <summary>
 /// The information returned when calling the FSync command
 /// </summary>
 public GetOpTimeResult(BsonObject document)
     : base(document)
 {
 }
Beispiel #9
0
 /// <summary>
 /// Manually invokes a command against the database
 /// </summary>
 public MethodResult RunCommand(BsonObject arguments)
 {
     return this._Database.RunCommand(arguments);
 }
 /// <summary>
 /// Creates details about the stats for a collection
 /// </summary>
 public CollectionStatsResult(BsonObject document)
     : base(document)
 {
 }
 /// <summary>
 /// Count information for the specified collection
 /// </summary>
 public CollectionCountResult(BsonObject document)
     : base(document)
 {
 }
Beispiel #12
0
        public static object[] Deserialize(byte[] bsonData, AssemblyLoader assembly)
        {
            using (MemoryStream memoryStream = new MemoryStream(bsonData))
            {
                object[] methodParams = null;
                using (BsonReader reader = new BsonReader(memoryStream))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    BsonObject     bsonObject = serializer.Deserialize <BsonObject>(reader);

                    List <string> types = JsonConvert.DeserializeObject <List <string> >(bsonObject.Payload[0].ToString());

                    methodParams = new object[types.Count];
                    for (int i = 1; i < bsonObject.Payload.Length; i++)
                    {
                        Type objectType = bsonObject.Payload[i].GetType();
                        if (objectType == typeof(JArray))
                        {
                            string type = types[i - 1];
                            if (type.Contains("List"))
                            {
                                Type listType            = typeof(List <>);
                                Type genericType         = assembly.ResolveClass(ExtractGenerics(type));
                                Type constructedListType = listType.MakeGenericType(genericType);

                                methodParams[i - 1] = JsonConvert.DeserializeObject(bsonObject.Payload[i].ToString(), constructedListType);
                            }
                            else
                            {
                                //TODO: map and others collections!
                            }
                        }
                        else if (objectType != Type.GetType(types[i - 1]))
                        {
                            switch (types[i - 1])
                            {
                            case "System.Int32":
                                methodParams[i - 1] = Convert.ToInt32(bsonObject.Payload[i]);
                                break;

                            case "System.UInt32":
                                methodParams[i - 1] = Convert.ToUInt32(bsonObject.Payload[i]);
                                break;

                            case "System.Int16":
                                methodParams[i - 1] = Convert.ToUInt16(bsonObject.Payload[i]);
                                break;

                            case "System.UInt16":
                                methodParams[i - 1] = Convert.ToUInt16(bsonObject.Payload[i]);
                                break;

                            case "System.Double":
                                methodParams[i - 1] = Convert.ToDouble(bsonObject.Payload[i]);
                                break;

                            case "System.Single":
                                methodParams[i - 1] = Convert.ToSingle(bsonObject.Payload[i]);
                                break;
                            }
                        }
                        else
                        {
                            methodParams[i - 1] = bsonObject.Payload[i];
                        }
                    }
                }
                return(methodParams);
            }
        }
Beispiel #13
0
 /// <summary>
 /// Creates a new MethodResult from the provided document
 /// </summary>
 public MethodResult(BsonObject document)
 {
     this.HasResponse = document is BsonDocument;
     this.Response = document ?? new BsonDocument();
 }
Beispiel #14
0
 /// <summary>
 /// The information returned when calling the FSync command
 /// </summary>
 public GetNonceResult(BsonObject document)
     : base(document)
 {
 }
 /// <summary>
 /// The result when trying to remove a collection
 /// </summary>
 public DropCollectionResult(BsonObject document)
     : base(document)
 {
 }
Beispiel #16
0
        public void Document_Create()
        {
            var now = DateTime.Now;
            var cid = Guid.NewGuid();

            // create a typed object
            var orderObject = new Order
            {
                OrderKey   = 123,
                CustomerId = cid,
                Date       = now,
                Items      = new List <OrderItem>()
                {
                    new OrderItem {
                        Qtd = 3, Description = "Package", Unit = 99m
                    }
                }
            };

            // create same object, but using BsonDocument
            var orderDoc = new BsonDocument();

            orderDoc.Id            = 123;
            orderDoc["CustomerId"] = cid;
            orderDoc["Date"]       = now;
            orderDoc["Items"]      = new BsonArray();
            var i = new BsonObject();

            i["Qtd"]         = 3;
            i["Description"] = "Package";
            i["Unit"]        = 99m;
            orderDoc["Items"].AsArray.Add(i);

            // serialize both and get indexKey for each one
            var bytesObject = BsonSerializer.Serialize(orderObject);
            var keyObject   = new IndexKey(BsonSerializer.GetIdValue(orderObject));

            var bytesDoc = BsonSerializer.Serialize(orderDoc);
            var keyDoc   = new IndexKey(BsonSerializer.GetIdValue(orderDoc));

            // lets revert objects (create a object from Document and create a Document from a object)
            var revertObject = BsonSerializer.Deserialize <Order>(keyDoc, bytesDoc);
            var revertDoc    = BsonSerializer.Deserialize <BsonDocument>(keyObject, bytesObject);

            // lets compare properties

            Assert.AreEqual(revertObject.OrderKey, revertDoc.Id);
            Assert.AreEqual(revertObject.CustomerId, revertDoc["CustomerId"].AsGuid);
            Assert.AreEqual(revertObject.Date, revertDoc["Date"].AsDateTime);
            Assert.AreEqual(revertObject.Items[0].Unit, revertDoc["Items"][0]["Unit"].AsDecimal);

            // get some property
            Assert.AreEqual(now, BsonSerializer.GetFieldValue(revertObject, "Date"));
            Assert.AreEqual(now, BsonSerializer.GetFieldValue(revertDoc, "Date"));

            Assert.AreEqual(cid, BsonSerializer.GetFieldValue(revertObject, "CustomerId"));
            Assert.AreEqual(cid, BsonSerializer.GetFieldValue(revertDoc, "CustomerId"));

            Assert.AreEqual(null, BsonSerializer.GetFieldValue(revertObject, "Date2"));
            Assert.AreEqual(null, BsonSerializer.GetFieldValue(revertDoc, "Date2"));
        }
Beispiel #17
0
 /// <summary>
 /// Creates a new container for the error result
 /// </summary>
 public ForceErrorResult(BsonObject document)
     : base(document)
 {
 }
 /// <summary>
 /// Creates a container for the last error message found
 /// </summary>
 public GetPreviousErrorResult(BsonObject document)
     : base(document)
 {
 }
Beispiel #19
0
 /// <summary>
 /// Manually invokes a command against the database
 /// </summary>
 public MethodResult RunCommand(BsonObject arguments, bool expectResponse)
 {
     return this._Database.RunCommand(arguments, expectResponse);
 }
 /// <summary>
 /// Executes a command against the database using the provided information
 /// </summary>
 public static CommandResponse RunCommand(MongoDatabase database, BsonObject parameters)
 {
     return MongoDatabaseCommands.RunCommand(database, parameters, true);
 }
 /// <summary>
 /// The information returned when calling the FSync command
 /// </summary>
 public DeleteCollectionIndexResult(BsonObject document)
     : base(document)
 {
 }
        /// <summary>
        /// Executes a command against the database using the provided information
        /// </summary>
        public static CommandResponse RunCommand(MongoDatabase database, BsonObject parameters, bool expectResponse)
        {
            //create the command to use
            CommandRequest request = new CommandRequest(database);
            request.Arguments.Merge(parameters);

            //send the command and check for the result
            CommandResponse response = database.SendRequest(request) as CommandResponse;
            if (response == null && expectResponse) {
                throw new MongoServerException(
                    string.Format(
                        "The request to {0} expected a response but nothing was returned!",
                        database.Connection.Host
                        ));
            }

            //return any documents that were found
            return response;
        }
Beispiel #23
0
        public static object Deserialize(byte[] bsonData)
        {
            using (MemoryStream memoryStream = new MemoryStream(bsonData))
            {
                object objReturn = null;
                using (BsonReader reader = new BsonReader(memoryStream))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    BsonObject     bsonObject = serializer.Deserialize <BsonObject>(reader);

                    string type       = bsonObject.Payload[0] as string;
                    Type   objectType = bsonObject.Payload[1].GetType();
                    if (objectType == typeof(JArray))
                    {
                        if (type.Contains("List"))
                        {
                            Type genericType = Type.GetType(type);
                            objReturn = JsonConvert.DeserializeObject(bsonObject.Payload[1].ToString(), genericType);
                        }
                        else
                        {
                            //TODO: map and others collections!
                        }
                    }
                    else if (objectType != Type.GetType(type))
                    {
                        switch (type)
                        {
                        case "System.Int32":
                            objReturn = Convert.ToInt32(bsonObject.Payload[1]);
                            break;

                        case "System.UInt32":
                            objReturn = Convert.ToUInt32(bsonObject.Payload[1]);
                            break;

                        case "System.Int16":
                            objReturn = Convert.ToUInt16(bsonObject.Payload[1]);
                            break;

                        case "System.UInt16":
                            objReturn = Convert.ToUInt16(bsonObject.Payload[1]);
                            break;

                        case "System.Double":
                            objReturn = Convert.ToDouble(bsonObject.Payload[1]);
                            break;

                        case "System.Single":
                            objReturn = Convert.ToSingle(bsonObject.Payload[1]);
                            break;
                        }
                    }
                    else
                    {
                        objReturn = bsonObject.Payload[1];
                    }
                }
                return(objReturn);
            }
        }
        /// <summary>
        /// Copies a database from one location to another
        /// </summary>
        public static MethodResult CopyDatabase(MongoDatabase database, string from, string to, string host, int port, string username, string password)
        {
            //create the request
            BsonObject parameters = new BsonObject(new {
                copydb = Mongo.CommandArgument,
                fromdb = from,
                todb = to,
                fromhost = host
            });

            //check for credentials
            username = (username ?? string.Empty).Trim();
            password = (password ?? string.Empty);

            //check for credentials
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) {

                //get the connection information for the target database
                using (MongoDatabase target = new MongoDatabase(to, host)) {
                    GetNonceResult nonce = MongoDatabaseCommands.GetNonce(target);
                    string key = MongoDatabaseCommands.HashPassword(username, password, nonce.Nonce);

                    //and add it to the command
                    parameters += new {
                        username = username,
                        nonce = nonce,
                        key = key
                    };

                }

            }

            //finally, with the created value, send the command
            CommandResponse response = MongoDatabaseCommands.RunCommand(database, parameters);
            return new MethodResult(response.GetDefaultResponse());
        }
 /// <summary>
 /// Creates the details for the distinct values in a collection
 /// </summary>
 public CollectionDistinctResult(BsonObject document)
     : base(document)
 {
 }
Beispiel #26
0
 /// <summary>
 /// The information returned when calling the FSync command
 /// </summary>
 public FSyncResult(BsonObject document)
     : base(document)
 {
 }
Beispiel #27
0
 /// <summary>
 /// Creates a new container for an AssertInfo response
 /// </summary>
 public AssertInfoResult(BsonObject document)
     : base(document)
 {
 }
Beispiel #28
0
 /// <summary>
 /// Creates a new BuildInfo result from the provided document
 /// </summary>
 public BuildInfoResult(BsonObject document)
     : base(document)
 {
 }