public ActionResult <object> GetByDocumentId(string id, MongoDocument mongoDoc)
        {
            MongoClient client     = Helper.GetMongoClient(mongoDoc);
            var         database   = client.GetDatabase(mongoDoc.DatabaseName);
            var         collection = database.GetCollection <BsonDocument>(mongoDoc.CollectionName);

            BsonDocument document;

            ObjectId oid;

            if (ObjectId.TryParse(id, out oid))
            {
                document = collection.Find($"{{ _id: ObjectId('{id}') }}").FirstOrDefault();
            }
            else
            {
                document = collection.Find(x => x["_id"] == id).FirstOrDefault();
            }

            if (document == null)
            {
                return(null);
            }

            return(document.ToString());
        }
        /// <summary>
        /// Creates document.
        /// </summary>
        /// <param name="mongoDoc">The mongo doc.</param>
        /// <returns>
        /// The document.
        /// </returns>
        public ActionResult <object> CreateDocument(MongoDocument mongoDoc)
        {
            MongoClient client     = Helper.GetMongoClient(mongoDoc);
            var         database   = client.GetDatabase(mongoDoc.DatabaseName);
            var         collection = database.GetCollection <BsonDocument>(mongoDoc.CollectionName);

            BsonDocument document = null;

            if (!string.IsNullOrWhiteSpace(mongoDoc.Id))
            {
                ObjectId oid;
                if (ObjectId.TryParse(mongoDoc.Id, out oid))
                {
                    document = collection.Find($"{{ _id: ObjectId('{mongoDoc.Id}') }}").FirstOrDefault();
                }
                else
                {
                    document = collection.Find(x => x["_id"] == mongoDoc.Id).FirstOrDefault();
                }
            }

            if (document == null)
            {
                using (var reader = new MongoDB.Bson.IO.JsonReader(mongoDoc.DocumentData))
                {
                    var context = BsonDeserializationContext.CreateRoot(reader);
                    document = BsonDocumentSerializer.Instance.Deserialize(context);
                }
                collection.InsertOne(document);
            }

            var doc = document.ToString();

            return(CreatedAtAction(nameof(GetByDocumentId), new { id = document["_id"].ToString(), mongoDoc = mongoDoc }, doc));
        }
        public ActionResult Index()
        {
            MongoDocument post = new MongoDocument();

            post.Title  = "AAAA";
            post.Url    = "BBB";
            post.Author = "Shen";
            _repository.Add <MongoDocument>(post);
            return(null);
        }
Exemple #4
0
        public void Save(MongoDocument document)
        {
            IMongoDatabase database = mongo.GetDatabase("web-diff");
            IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>(document.GetCollectionName());

            if (document._id == null)
            {
                collection.InsertOne(document.ToBsonDocument());
            }
            else
            {
                collection.FindOneAndReplace(
                    new BsonDocument {
                    { "_id", document._id }
                },
                    document.ToBsonDocument()
                    );
            }
        }
Exemple #5
0
        /// <summary>
        /// Handles reading back the content for this query
        /// </summary>
        protected override void ParseStream(Stream stream)
        {
            var reader = new BinaryReader(stream);

            /* Message Format (Excluding Header)
             * Flag (int32) - normally zero, non-zero on query failure
             * CursorId (int64) - id of the cursor created for this query response
             * StartingFrom (int32) - indicates where in this content is starting from
             * TotalReturned (int32) - number of documents in the reply
             * Documents (BSON array) - The documents (parsed separately)
             */

            //get the flag first
            Flag = reader.ReadInt32();

            //second is the cursor value
            CursorId = reader.ReadInt64();

            //and the skipped and returned counts
            StartingFrom = reader.ReadInt32();
            TotalReturned = reader.ReadInt32();

            //next, read and parse the records
            Documents = new List<MongoDocument>();
            for (var i = 0; i < TotalReturned; i++) {

                //convert to a MongoDocument
                var parsed = BsonDocument.FromStream(stream);
                var document = new MongoDocument();
                document.Merge(parsed);
                //when the parsed doc has a field _id which is not MongOid, it ends up being replaced
                //as a fix, rename it as id
                if (parsed.Has(Mongo.DocumentIdKey) && parsed[Mongo.DocumentIdKey].GetType() != typeof(MongoOid))
                {
                    document["id"] = parsed[Mongo.DocumentIdKey];
                }

                //and add it to the list
                Documents.Add(document);
            }

            //check for server exceptions
            CheckForExceptions();
        }
Exemple #6
0
        /// <summary>
        /// Handles reading back the content for this query
        /// </summary>
        protected override void ParseStream(Stream stream)
        {
            BinaryReader reader = new BinaryReader(stream);

            /* Message Format (Excluding Header)
             * Flag (int32) - normally zero, non-zero on query failure
             * CursorId (int64) - id of the cursor created for this query response
             * StartingFrom (int32) - indicates where in this content is starting from
             * TotalReturned (int32) - number of documents in the reply
             * Documents (BSON array) - The documents (parsed separately)
             */

            //get the flag first
            this.Flag = reader.ReadInt32();

            //second is the cursor value
            this.CursorId = reader.ReadInt64();

            //and the skipped and returned counts
            this.StartingFrom = reader.ReadInt32();
            this.TotalReturned = reader.ReadInt32();

            //next, read and parse the records
            this.Documents = new List<MongoDocument>();
            for (int i = 0; i < this.TotalReturned; i++) {

                //convert to a MongoDocument
                BsonDocument parsed = BsonDocument.FromStream(stream);
                MongoDocument document = new MongoDocument();
                document.Merge(parsed);

                //and add it to the list
                this.Documents.Add(document);
            }

            //check for server exceptions
            this.CheckForExceptions();
        }