示例#1
0
        public void Can_save_and_load_document()
        {
            _collection.Save(_origin, false);

            var id = _origin.GetBsonValue("_id");

            var reloaded = _collection.Load((ObjectId)id.Value);

            //TODO: made more string assertion
            Assert.That(reloaded, Is.Not.Null);
        }
示例#2
0
        /// <summary>
        /// Saves document to collection
        /// </summary>
        /// <param name="doc">Document to save</param>
        /// <param name="merge">If true the merge will be performed with old and new objects. Otherwise old object will be replaced</param>
        public unsafe ObjectId Save(BsonDocument doc, bool merge)
        {
            BsonValue id = doc.GetBsonValue("_id");

            ObjectId oiddata = new ObjectId();

            if (id != null)
            {
                oiddata = (ObjectId)id.Value;
            }

            using (var stream = Database.StreamPool.GetStream())
            {
                doc.Serialize(stream);
                bool saveOk = false;
                fixed(byte *streamPointer = &stream.GetBuffer()[0])
                {
                    saveOk = _functions.SaveBson(CollectionHandle, streamPointer, &oiddata, merge);
                }

                if (saveOk && id == null)
                {
                    doc.SetOID("_id", oiddata);
                }

                if (!saveOk)
                {
                    throw EjdbException.FromDatabase(Database, "Failed to save Bson");
                }
            }

            return(oiddata);
        }