mongodb-.net-driver

MongoDB geospatial index in C#

本小妞迷上赌 提交于 2019-12-06 04:52:51
问题 I have been trying to get started but run into the same rock time after time trying to create and query MongoDB with C# official driver. The problem is how to create data with geo information. I am just not finding the answer. Code: MongoUrl url = new MongoUrl("mongodb://xxx.xx.x.xx/mydb"); MongoServer server = MongoServer.Create(url); MongoDatabase database = server.GetDatabase("mydb"); <-- this works fine BsonDocument[] batch = { new BsonDocument { { "name", "Bran" }, { "loc", "10, 10" } },

Is there a way to create or update a MongoDB index?

独自空忆成欢 提交于 2019-12-06 03:49:10
According to the documentation on the createIndexes command : If you create an index with one set of options and then issue createIndexes with the same index fields but different options, MongoDB will not change the options nor rebuild the index. The solution is to drop the index and create it from scratch, but that's costly. Is there a way to create an index when there isn't one, do nothing if there is an index with the same options, and replace the index if the options have changed? This question was originally raised by Phil Barresi here but has since been deleted. I had the same problem,

How to set MongoDB Change Stream 'OperationType' in the C# driver?

淺唱寂寞╮ 提交于 2019-12-06 02:05:29
问题 When running the new MongDB Server, version 3.6, and trying to add a Change Stream watch to a collection to get notifications of new inserts and updates of documents, I only receive notifications for updates, not for inserts. This is the default way I have tried to add the watch: IMongoDatabase mongoDatabase = mongoClient.GetDatabase("Sandbox"); IMongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>("TestCollection"); var changeStream = collection.Watch()

C# MongoDb serialization of Dictionary<string, object>

删除回忆录丶 提交于 2019-12-05 22:19:14
I have a collection in my database where I log events. Each type of event has a different set of data. I've defined this with the following class: [CollectionName("LogEvent")] public class LogEvent { public LogEvent(string eventType) { EventType = eventType; EventData = new Dictionary<string, object>(); } public string EventType { get; private set; } [BsonExtraElements] public IDictionary<string, object> EventData { get; private set; } } Now - this works pretty good to some extent. As long as the elements of the EventData dictionary are simple types... var event = new LogEvent("JobQueues"){

Mongodb .net async await

眉间皱痕 提交于 2019-12-05 20:59:01
Does the mongodb .net driver offer support for async/await operations? I can't seem to find any info on this. I'm looking for something like EntityFramework has: ToListAsync(), FindAsync(), CountAsync() Is this supported? 2.0 Driver is released. Check it out: https://github.com/mongodb/mongo-csharp-driver Nuget: https://www.nuget.org/packages/MongoDB.Driver I've made some changes in the official driver to make it async as possible. Basically, I've changed the MongoConnection class to use the NetworkStream async methods (WriteAsync and ReadAsync) and propagated the changes all by the code.

Retrieve data from mongodb using C# driver

北战南征 提交于 2019-12-05 18:16:27
问题 I'm using official mongodb driver for c# in my test project and i've already insert document from c# web application to mongodb. In mongo console, db.blog.find() can display entries I've inserted. but when i tried to retrieve them, .net throw a exception "System.InvalidOperationException: ReadString can only be called when CurrentBsonType is String, not when CurrentBsonType is ObjectId." my entity class is very simple namespace MongoDBTest { public class Blog { public String _id { get; set; }

How can I tell the MongoDB C# driver to store all Guids in string format?

被刻印的时光 ゝ 提交于 2019-12-05 17:18:46
I'm currently applying the [BsonRepresentation(BsonType.String)] attribute to all Guid properties in my domain models to have those properties serialized in string format. Besides being tiresome to do, that doesn't work out sometimes, e.g. with custom Wrapper<T> classes: public class Wrapper<T> { public T Value { get; set; } // Further properties / business logic ... } When T is Guid , the Value property will be stored as binary data of type UuidLegacy (as will any property of type Guid that's not decorated with the above attribute). However, I'd like all Guid s, including Wrapper<Guid>.Value

How do I set the serialization options for the geo values using the official 10gen C# driver?

霸气de小男生 提交于 2019-12-05 17:08:21
Considering this class: public class Location { public Coordinates Geo { get; set; } public Location() { Geo = new Coordinates(); } public class Coordinates { public decimal Lat { get; set; } public decimal Long { get; set; } } } I have a geospatial index on the collection set like { Geo: "2d" } . Unfortunately the driver tries to store lat/lon coordinates as strings, instead of numbers and I get an error that says Tue Mar 15 16:29:22 [conn8] insert database.locations exception 13026 geo values have to be numbers: { Lat: "50.0853779", Long: "19.931276700000012" } 1ms . To alleviate this

FindAll in MongoDB .NET Driver 2.0

[亡魂溺海] 提交于 2019-12-05 16:23:31
问题 I want to query my MongoDB collection without any filter with MongoDB .NET Driver 2.0 but I didn't find a way. I have the following workaround but it looks weird :D var filter = Builders<FooBar>.Filter.Exists(x => x.Id); var fooBars = await _fooBarCollection.Find(filter) .Skip(0) .Limit(100) .ToListAsync(); Is there a way to issue queries without a filter in MongoDB .NET Driver 2.0? 回答1: You can't use Find without a filter. You can however use a filter that passes everything: var findFluent =

MongoDB : update entire document except _id using C# driver

好久不见. 提交于 2019-12-05 15:48:26
I have to update all the fields except _id. I want to avoid to manually update the 16 fields... All the new fields are stored inside a BsonDocument Thanks for ideas As @Philipp hinted there is a way way to do this. You can actually use the save function ( http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Save%3CTDocument%3Emethod ) which will do what he says for you in the database end. So imagine you have a document of: { _id: {}, d: 1 } And that _id already exists, it will replace the previous document with this one. Neat huh? When I understood you correctly,