Exemple #1
0
        void treeListView1_CellEditFinishing(object sender, BrightIdeasSoftware.CellEditEventArgs e)
        {
            if (e.Cancel)
            {
                return;
            }
            if (e.NewValue.Equals(e.Value))
            {
                return;
            }

            var node = e.RowObject as ResultTreeNodeModel;

            if (node != null && node.IsValue)
            {
                var convertedValue = node.BsonType.Create(e.NewValue);
                if ((convertedValue == null || convertedValue == BsonNull.Value) && node.BsonType != BsonNull.Value)
                {
                    e.Cancel = true;
                    return;
                }

                try
                {
                    var    parts      = node.BsonUpdateQuery.Split(new[] { '.' });
                    string _id        = parts[0];
                    var    collection = _mongoCollectionInfo.GetMongoCollection();
                    var    query      = GetIdQuery(_id);
                    var    bDoc       = collection.FindOneAs <BsonDocument>(query);
                    if (bDoc != null)
                    {
                        var u      = node.BsonUpdateQuery.Substring(node.BsonUpdateQuery.IndexOf('.') + 1);
                        var update = MongoDB.Driver.Builders.Update.Set(u, convertedValue);

                        var args = new FindAndModifyArgs {
                            Query = query, Update = update
                        };
                        collection.FindAndModify(args);

                        node.Value = e.NewValue.ToString();
                    }
                    else
                    {
                        MessageBox.Show("Unexpected trouble finding document by id.", "Update Failed");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to update document.\r\n\r\n" + ex.Message, "Update Error");
                }
            }
        }
Exemple #2
0
        private void CreateIndexJsonMode()
        {
            string indexJson = textBoxIndexJson.Text.Trim();

            if (string.IsNullOrWhiteSpace(indexJson))
            {
                throw new Exception("Enter index json.");
            }

            IndexKeysDocument keys = null;

            try
            {
                keys = BsonSerializer.Deserialize <IndexKeysDocument>(indexJson);
            }
            catch (Exception ex1)
            {
                throw new Exception($"Invalid index json.\r\n\r\n{ex1.Message}");
            }

            string optionJson            = textBoxIndexOptionsJson.Text.Trim();
            IndexOptionsDocument options = null;

            if (string.IsNullOrWhiteSpace(optionJson))
            {
                options = new IndexOptionsDocument();
            }
            else
            {
                try
                {
                    options = BsonSerializer.Deserialize <IndexOptionsDocument>(optionJson);
                }
                catch (Exception ex1)
                {
                    throw new Exception($"Invalid options json.\r\n\r\n{ex1.Message}");
                }
            }

            if (!options.Contains("name"))
            {
                string indexName = txtBoxIndexName.Text;
                if (string.IsNullOrWhiteSpace(indexName))
                {
                    throw new Exception("Please enter an index name.");
                }

                options.Add("name", indexName);
            }

            if (!options.Contains("unique"))
            {
                options.Add("unique", checkBoxUnique.Checked);
            }
            if (!options.Contains("dropDups") && checkBoxUnique.Checked)
            {
                options.Add("dropDups", checkBoxDropDups.Checked);
            }
            if (!options.Contains("background"))
            {
                options.Add("background", checkBoxBackground.Checked);
            }
            if (!options.Contains("sparse"))
            {
                options.Add("sparse", checkBoxSparse.Checked);
            }

            var mongoCollection = MongoCollectionInfo.GetMongoCollection();
            WriteConcernResult writeConcernResult = mongoCollection.CreateIndex(keys, options);

            if (writeConcernResult.HasLastErrorMessage)
            {
                throw new Exception(writeConcernResult.LastErrorMessage);
            }
        }
Exemple #3
0
        private void CreateIndexBasicMode()
        {
            string indexName = txtBoxIndexName.Text;

            if (string.IsNullOrWhiteSpace(indexName))
            {
                throw new Exception("Please enter an index name.");
            }

            var mongoCollection = MongoCollectionInfo.GetMongoCollection();
            var indexes         = mongoCollection.GetIndexes();

            if (indexes != null && indexes.Any())
            {
                if (indexes.ToList().Exists(i => i.Name == indexName))
                {
                    throw new Exception("An index with that name already exists.");
                }
            }

            var keys = GetChoosenKeys();

            if (!keys.Any())
            {
                throw new Exception("You must choose at least one key.");
            }

            var keyBuilder = new IndexKeysBuilder();

            foreach (var key in keys)
            {
                if (key.SortType == 1)
                {
                    keyBuilder = keyBuilder.Ascending(key.Key);
                }
                else if (key.SortType == -1)
                {
                    keyBuilder = keyBuilder.Descending(key.Key);
                }
                else if (key.SortType == 2)
                {
                    keyBuilder = keyBuilder.GeoSpatial(key.Key);
                }
                else if (key.SortType == 3)
                {
                    keyBuilder = keyBuilder.GeoSpatialHaystack(key.Key);
                }
            }
            var optionBuilder = new IndexOptionsBuilder();

            optionBuilder.SetUnique(checkBoxUnique.Checked);
            optionBuilder.SetBackground(checkBoxBackground.Checked);
            optionBuilder.SetDropDups(checkBoxUnique.Checked && checkBoxDropDups.Checked);
            optionBuilder.SetSparse(checkBoxSparse.Checked);
            optionBuilder.SetName(indexName);

            var writeConcernResult = mongoCollection.CreateIndex(keyBuilder, optionBuilder);

            if (writeConcernResult.HasLastErrorMessage)
            {
                throw new Exception(writeConcernResult.LastErrorMessage);
            }
        }