public void TestFindNearSphericalFalse() { if (collection.Exists()) { collection.Drop(); } collection.Insert(new Place { Location = new[] { -74.0, 40.74 }, Name = "10gen", Type = "Office" }); collection.Insert(new Place { Location = new[] { -75.0, 40.74 }, Name = "Two", Type = "Coffee" }); collection.Insert(new Place { Location = new[] { -74.0, 41.73 }, Name = "Three", Type = "Coffee" }); collection.CreateIndex(IndexKeys.GeoSpatial("Location")); var query = Query.Near("Location", -74.0, 40.74); var hits = collection.Find(query).ToArray(); Assert.AreEqual(3, hits.Length); var hit0 = hits[0]; Assert.AreEqual(-74.0, hit0["Location"].AsBsonArray[0].AsDouble); Assert.AreEqual(40.74, hit0["Location"].AsBsonArray[1].AsDouble); Assert.AreEqual("10gen", hit0["Name"].AsString); Assert.AreEqual("Office", hit0["Type"].AsString); // with spherical false "Three" is slightly closer than "Two" var hit1 = hits[1]; Assert.AreEqual(-74.0, hit1["Location"].AsBsonArray[0].AsDouble); Assert.AreEqual(41.73, hit1["Location"].AsBsonArray[1].AsDouble); Assert.AreEqual("Three", hit1["Name"].AsString); Assert.AreEqual("Coffee", hit1["Type"].AsString); var hit2 = hits[2]; Assert.AreEqual(-75.0, hit2["Location"].AsBsonArray[0].AsDouble); Assert.AreEqual(40.74, hit2["Location"].AsBsonArray[1].AsDouble); Assert.AreEqual("Two", hit2["Name"].AsString); Assert.AreEqual("Coffee", hit2["Type"].AsString); query = Query.Near("Location", -74.0, 40.74, 0.5); // with maxDistance hits = collection.Find(query).ToArray(); Assert.AreEqual(1, hits.Length); hit0 = hits[0]; Assert.AreEqual(-74.0, hit0["Location"].AsBsonArray[0].AsDouble); Assert.AreEqual(40.74, hit0["Location"].AsBsonArray[1].AsDouble); Assert.AreEqual("10gen", hit0["Name"].AsString); Assert.AreEqual("Office", hit0["Type"].AsString); query = Query.Near("Location", -174.0, 40.74, 0.5); // with no hits hits = collection.Find(query).ToArray(); Assert.AreEqual(0, hits.Length); }
public CSharp801() { _collection = LegacyTestConfiguration.GetCollection <C>(); if (_collection.Exists()) { _collection.Drop(); } }
public void SetUp() { _collection = LegacyTestConfiguration.GetCollection <C>(); if (_collection.Exists()) { _collection.Drop(); } }
/// <summary> /// Initializes a new instance of the <see cref="MongoRepository{TDocument}" /> class. /// </summary> /// <param name="database">The database.</param> /// <param name="collectionName">Name of the collection.</param> public MongoRepository(MongoDatabase database, string collectionName) { Collection = database.GetCollection <TDocument>(collectionName); if (!Collection.Exists()) { Collection.Database.CreateCollection(Collection.Name); } }
public CSharp840() { _collection = LegacyTestConfiguration.GetCollection<BsonDocument>(); if (_collection.Exists()) { _collection.Drop(); } _collection.Insert(new BsonDocument { { "x", 1 } }); _collection.Insert(new BsonDocument { { "x", 2 }, { "Length", BsonNull.Value } }); _collection.Insert(new BsonDocument { { "x", 3 }, { "Length", 123 } }); }
public void SetUp() { _collection = Configuration.GetTestCollection<BsonDocument>(); if (_collection.Exists()) { _collection.Drop(); } _collection.Insert(new BsonDocument { { "x", 1 } }); _collection.Insert(new BsonDocument { { "x", 2 }, { "Length", BsonNull.Value } }); _collection.Insert(new BsonDocument { { "x", 3 }, { "Length", 123 } }); }
public void Context() { var connectionStringUrl = new MongoUrl("mongodb://localhost/integration_tests"); MongoDatabase mongoDatabase = MongoDatabase.Create(connectionStringUrl); mongoCollection = mongoDatabase.GetCollection("end_to_end_tests"); if(mongoCollection.Exists()) { mongoCollection.Drop(); } var elementWithIEnumerableProperty = new ElementWithIEnumerableProperty{Name = "Test", Objects = new List<object>{"Test value for serialization"}.Select(x =>x)}; mongoCollection.Insert(elementWithIEnumerableProperty); }
public static void TransfertOldLastId(string fromCollection, string fromDatabase, string toCollection = null, string toDatabase = null, string IdGeneratorCollection = "IdGenerator", string fromServer = null, string toServer = null) { if (toCollection == null) { toCollection = fromCollection; } if (toDatabase == null) { toDatabase = fromDatabase; } Trace.WriteLine("transfert last id from collection \"{0}\" last id collection \"{1}\" database \"{2}\" server \"{3}\" to collection \"{4}\" database \"{5}\" server \"{6}\"", IdGeneratorCollection, fromCollection, fromDatabase, fromServer, toCollection, toDatabase, toServer); MongoCollection collection = zMongoDB.GetCollection(fromDatabase, IdGeneratorCollection, fromServer); if (!collection.Exists()) { throw new PBException("collection not found \"{0}\" database \"{1}\" server \"{2}\"", IdGeneratorCollection, fromDatabase, fromServer); } BsonDocument doc = collection.FindOneAs <BsonDocument>(new QueryDocument { { "collection", fromCollection } }); if (doc == null) { throw new PBException("last id not found for collection \"{0}\"", fromCollection); } int lastId = doc["lastId"].AsInt32; collection = zMongoDB.GetCollection(toDatabase, toCollection, toServer); doc = collection.FindOneAs <BsonDocument>(new QueryDocument { { "_id", 0 } }); if (doc != null) { throw new PBException("document _id 0 exists already in collection \"{0}\" database \"{1}\" server \"{2}\"", toCollection, toDatabase, toServer); } doc = new BsonDocument { { "_id", 0 }, { "LastId", lastId } }; WriteConcernResult result = collection.Insert(doc); if (result.Ok) { Trace.WriteLine("last id transfered to collection \"{0}\" database \"{1}\" server \"{2}\"", toCollection, toDatabase, toServer); Trace.WriteLine(doc.zToJson()); } else { throw new PBException("unknow error inserting last id to collection \"{0}\" database \"{1}\" server \"{2}\"", toCollection, toDatabase, toServer); } }
public void TestEnsureIndexAfterDropCollection() { if (_collection.Exists()) { _collection.Drop(); } Assert.IsFalse(_collection.IndexExists("x")); _collection.EnsureIndex("x"); Assert.IsTrue(_collection.IndexExists("x")); _collection.Drop(); Assert.IsFalse(_collection.IndexExists("x")); _collection.EnsureIndex("x"); Assert.IsTrue(_collection.IndexExists("x")); }
public void TestCreateIndexAfterDropCollection() { if (_collection.Exists()) { _collection.Drop(); } Assert.False(_collection.IndexExists("x")); _collection.CreateIndex("x"); Assert.True(_collection.IndexExists("x")); _collection.Drop(); Assert.False(_collection.IndexExists("x")); _collection.CreateIndex("x"); Assert.True(_collection.IndexExists("x")); }
public void SetUp() { _collection = LegacyTestConfiguration.GetCollection <BsonDocument>(); if (_collection.Exists()) { _collection.Drop(); } _collection.Insert(new BsonDocument { { "x", 1 } }); _collection.Insert(new BsonDocument { { "x", 2 }, { "Length", BsonNull.Value } }); _collection.Insert(new BsonDocument { { "x", 3 }, { "Length", 123 } }); }
public void TestEnsureIndexAfterDropCollection() { if (collection.Exists()) { collection.Drop(); } server.ResetIndexCache(); Assert.IsFalse(collection.IndexExists("x")); collection.EnsureIndex("x"); Assert.IsTrue(collection.IndexExists("x")); collection.Drop(); Assert.IsFalse(collection.IndexExists("x")); collection.EnsureIndex("x"); Assert.IsTrue(collection.IndexExists("x")); }
public static Task <Boolean> ExistsAsync(this MongoCollection collection) { var tcs = new TaskCompletionSource <Boolean>(); ThreadPool.QueueUserWorkItem(_ => { try { var result = collection.Exists(); tcs.SetResult(result); } catch (Exception exc) { tcs.SetException(exc); } }); return(tcs.Task); }
/// <summary> /// 是否有表数据 /// </summary> /// <param name="collectionName">表名</param> /// <returns>是否有数据</returns> public bool ExistsData(string collectionName) { MongoCollection collection = GetMongoCollection(collectionName); if (!collection.Exists()) { return(false); } if (collection.Count() == 0) { return(false); } else { return(true); } }
/// <summary> /// 清除表数据 /// </summary> /// <param name="collectionName">表名</param> /// <returns>是否成功</returns> public bool ClearDataFromCollection(string collectionName) { MongoCollection collection = GetMongoCollection(collectionName); if (!collection.Exists()) { return(true); } if (collection.Count() == 0) { return(true); } WriteConcernResult concernResult = collection.RemoveAll(); if (concernResult == null) { return(false); } return(concernResult.Ok); }
public bool Exists() { return(_collection.Exists()); }
public bool HasTopics(Identity id) { return(topicsCollection.Exists(QueryGetByGroup(id))); }
public bool setup(string connectionString, string databaseString, string collectionString) { _client = new MongoClient(connectionString); _server = _client.GetServer(); _database = _server.GetDatabase(databaseString); _collection = _database.GetCollection<Entity>(collectionString); //If Collection exists, then everything else must exist if (_collection.Exists()) { return true; } else { return false; } }
public bool HasChilds(Identity id) { return(groupCollection.Exists(BuildQuery(id))); }
/// <summary> /// Tests whether this collection exists. /// </summary> /// <returns>True if this collection exists.</returns> public virtual bool Exists() { return(_collection.Exists()); }
public void SetUp() { _collection = LegacyTestConfiguration.GetCollection<C>(); if (_collection.Exists()) { _collection.Drop(); } }
private static void CreateCollection(MongoCollection <BsonDocument> sourceCollection, MongoCollection <BsonDocument> targetCollection, FlexibleOptions options) { if (targetCollection.Exists()) { return; } List <string> config = new List <string> (); if (!String.IsNullOrWhiteSpace(options.Get("collection-wt-configString"))) { config.AddRange(options.Get("collection-wt-configString", "").Split(',').Select(i => i.Trim()).Where(i => !String.IsNullOrEmpty(i))); } if (options.HasOption("collection-wt-block-compressor") && valid_wt_compressors.Contains(options.Get("collection-wt-block-compressor", "invalid"))) { config.RemoveAll(i => i.StartsWith("block_compressor=", StringComparison.OrdinalIgnoreCase)); config.Add("block_compressor=" + options.Get("collection-wt-block-compressor", "").ToLowerInvariant()); } if (!String.IsNullOrWhiteSpace(options.Get("collection-wt-allocation"))) { // Mongodb version 3.0.4 defaults to: "allocation_size=4KB,internal_page_max=4KB,leaf_page_max=32KB,leaf_value_max=1MB" if (options.Get("collection-wt-allocation") == "2x") { config.RemoveAll(i => i.StartsWith("allocation_size=", StringComparison.OrdinalIgnoreCase) || i.StartsWith("leaf_page_max=", StringComparison.OrdinalIgnoreCase) || i.StartsWith("internal_page_max=", StringComparison.OrdinalIgnoreCase)); config.Add("allocation_size=8KB"); config.Add("leaf_page_max=64KB"); config.Add("internal_page_max=8KB"); } else if (options.Get("collection-wt-allocation") == "4x") { config.RemoveAll(i => i.StartsWith("allocation_size=", StringComparison.OrdinalIgnoreCase) || i.StartsWith("leaf_page_max=", StringComparison.OrdinalIgnoreCase) || i.StartsWith("internal_page_max=", StringComparison.OrdinalIgnoreCase)); config.Add("allocation_size=16KB"); config.Add("leaf_page_max=64KB"); config.Add("internal_page_max=16KB"); } else if (options.Get("collection-wt-allocation") == "8x") { config.RemoveAll(i => i.StartsWith("allocation_size=", StringComparison.OrdinalIgnoreCase) || i.StartsWith("leaf_page_max=", StringComparison.OrdinalIgnoreCase) || i.StartsWith("internal_page_max=", StringComparison.OrdinalIgnoreCase)); config.Add("allocation_size=32KB"); config.Add("leaf_page_max=128KB"); config.Add("internal_page_max=32KB"); } } // apply configuration if (config.Count > 0) { try { var storageEngineDoc = new BsonDocument("wiredTiger", new BsonDocument("configString", String.Join(",", config))); targetCollection.Database.CreateCollection(targetCollection.Name, CollectionOptions.SetStorageEngineOptions(storageEngineDoc)); } catch (Exception ex) { NLog.LogManager.GetLogger("CreateCollection").Error(ex); } } }
public CSharp801() { _collection = LegacyTestConfiguration.GetCollection<C>(); if (_collection.Exists()) { _collection.Drop(); } }