mongodb-.net-driver

How to create Bson Document with Null value using C# official driver?

情到浓时终转凉″ 提交于 2019-12-30 18:08:48
问题 I have objects with 3 string fields Country, Province, City. They can contain null or some string name. I wanna query all data with the exact same values. For Example i need all data where City = null, Province = "WA", Country = "USA" I created BsonDocument: var lookup = new QueryDocument { {"GeoPosition.City", userLocation.City}, {"GeoPosition.Province", userLocation.Province}, {"GeoPosition.Country", userLocation.Country} }; But null field was thrown away and document looks like: {

MongoDb TTL on nested document is possible?

时光毁灭记忆、已成空白 提交于 2019-12-30 17:16:31
问题 I want to know if it's possible to use TTL on nested documents. Scenario I have Account and inside I have Sessions . Sessions need to expire in 30 minutes. I've set everything up but obviously when I set TTL index on Account.Sessions.EndDateTime it removes the whole Account . Can I make sure it removes only Session ? This is what it looks like in database. Notice how it will delete whole Account and not only Session when EndDateTime will come. { "_id" : ObjectId("53af273888dba003f429540b"),

Updating Dictionary in Mongodb

自作多情 提交于 2019-12-30 10:42:57
问题 I have a class that stores the following data: public class User { public ObjectId _id { get; set; } public string Name { get; set; } public string Pass { get; set; } public Dictionary<string, Tuple<string, string>> Quests { get; set; } } New Users are instantiated with this: await collection.InsertOneAsync(new User { Name = u, Pass = p, Quests = new Dictionary<string,Tuple<string,string>>() }); I know how to find and pull information from created documents, but I don't know how to push and

MongoDump query with BinData

馋奶兔 提交于 2019-12-30 09:32:42
问题 The Mongodump documentation specifies you can dump using a specific query i.e. mongodump --host localhost --db mydb --collection testCollection --query "{SomeKey: 'some value'}" I'm storing _ids fields as BinData, is it possible to query on this? I've tried mongodump --host localhost --db mydb --collection testCollection --query "{_id: 'BinData(3,ryBRQ+Px0kGRsZofJhHgqg==)'}" With no luck. 回答1: This needs a lot of escaping, unfortunately. Also, you'll have to use the $binary representation

Mongo C# driver 2.0 - Find count without fetching documents

独自空忆成欢 提交于 2019-12-30 07:03:16
问题 A general count query will be doing a int count = collection.Find(filter).Count(); Now that loads all the records as per the filter, so lets says I have 1 million records and out of those 0.5 million match my filter, so I'll have collection already filled with 0.5 documents. This is good enough if you want the documents, but what if you just want to know the count and not really need the documents, for memory sake. Can I do something like this int count = collection.Find(filter).SetLimit(1)

Insert element into nested array in Mongodb

情到浓时终转凉″ 提交于 2019-12-30 06:21:17
问题 I have this : { "_id" : ObjectId("4fb4fd04b748611ca8da0d48"), "Name" : "Categories", "categories" : [{ "_id" : ObjectId("4fb4fd04b748611ca8da0d46"), "name" : "SubCategory", "sub-categories" : [{ "_id" : ObjectId("4fb4fd04b748611ca8da0d47"), "name" : "SubSubCategory", "standards" : [] }] }] } I would like to add a new SubCategory using the C# driver. Is there an optimal way to do this? 回答1: You can do this using FindOneAndUpdateAsync and positional operator public async Task Add(string

Unknown discriminator value 'MyEvent'

吃可爱长大的小学妹 提交于 2019-12-29 08:32:39
问题 Using the MongoDB persistance engine in joliver/EventStore causing the error Unknown discriminator value 'MyEvent' . The issue is only caused when I try to load all events for replaying the events like this.storeEvent.Advanced.GetFrom(new DateTime(2010, 1,1)) The issues is caused in ExtensionsMethods.cs public class MyClassEvent : IDomainEvent { ... } public static Commit ToCommit(this BsonDocument doc, IDocumentSerializer serializer) { if (doc == null) return null; var id = doc["_id"]

MongoDB: A timeout occured after 30000ms selecting a server using CompositeServerSelector

∥☆過路亽.° 提交于 2019-12-29 08:10:10
问题 I'm completely stumped. I am using the latest c# drivers (2.3.0.157) and the latest MongoDB (3.2). The DB is running as a standalone setup with no replication or sharding. I've tried running locally on Windows as well as remotely on Amazon LINUX. I continue to get a timeout error but mysteriously sometimes it just works (maybe once every 20 - 30 attempts). I am creating the connection as such: private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["MongoDB"]

How to use MongoRegex (MongoDB C# Driver)

。_饼干妹妹 提交于 2019-12-29 01:52:09
问题 Has anyone have any idea how to use MongoRegex for the document search? I attempted this, but returns nothing back: var spec = new Document(); spec.Add("Name", new MongoRegex("/" + searchKey + "*/", "i")); collection.Find(spec) Wondering why it doesn't work, I tried to execute following command from the console: db.things.find({"Name":/john*/i}) /* WORKS */ db.things.find({"Name":"/john*/i"}) /* DOESN'T WORK */ Is that possible that the driver applies double quotation to the regex? Thanks..

MongoServer.State equivalent in the 2.0 driver

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-28 06:52:05
问题 In the old API (1.X) you could tell whether the server was connected or not by using the State property on the MongoServer instance returned from MongoClient.GetServer : public bool IsConnceted { get { return _client.GetServer().State == MongoServerState.Connected; } } However GetServer is not a part of the new API (2.0). How can that be achieved? 回答1: The more appropriate way to do that is not by checking the server but rather the cluster (which may contain multiple servers) and you can