mongodb-.net-driver

C# Mongo Driver IMongoDatabase RunCommand to get database stats

a 夏天 提交于 2021-02-19 02:43:05
问题 IMongoDatabase does not support db.GetStats(); which is deprecated in new version. I want to try alternate approach to get database stats. I use the following code to run command as we can get the stats from shell: var client = new MongoClient("mongodb://localhost:27017/analytics"); var db = client.GetDatabase("analytics"); var stats = db.RunCommand<BsonDocument>("db.stats()"); var collectionNames = db.RunCommand<BsonDocument> ("db.getCollectionNames()"); I am getting following error here:

C# MongoDB Driver: Can't find the way to run complex query for AnyIn filter in MongoDB

限于喜欢 提交于 2021-02-10 06:42:06
问题 I have a document like this: { "id": "xxxxxxxxxxxx", "groupsAuthorized": [ "USA/California/SF", "France/IDF/Paris" ] } And I have an user that has a list of authorized groups, like for example the following: "groups": [ "France/IDF", "USA/NY/NYC" ] What I'm trying to achieve is to retrieve all documents in the database that the user is authorized to retrieve, essentially I want to be able to check in the list "groupsAuthorized" if one of the group contains a subset of an element of the other

What is new way of setting DateTimeSerializationOptions.Defaults in mongodb c# driver?

一笑奈何 提交于 2021-02-08 15:50:53
问题 I was using this line to set the datetime defaults. DateTimeSerializationOptions.Defaults = DateTimeSerializationOptions.LocalInstance; I get this warning. 'MongoDB.Bson.Serialization.Options.DateTimeSerializationOptions.Defaults' is obsolete: 'Create and register a DateTimeSerializer with the desired options instead. ' But i couldn't find an example to change it... how can I change this obsolute usage? 回答1: Register the date/time serializer like this: BsonSerializer.RegisterSerializer(typeof

How to update deeply nested array with C# MongoDB.Driver?

落花浮王杯 提交于 2021-02-07 02:58:01
问题 I have such model: { "_id":"5b90eea8c02e062be2888446", "storeGuid":"e97d4730-9b8a-49ed-be87-caf4054439aa", "storeId":"0", "storeDbName":"0", "tenant":"dev", "configGroup":[ { "groupName":"peopleCounter", "config":[ { "key":"averageWaitPeriodTime", "value":"60", } ] }, { "groupName":"sessionMonitor", "config":[ { "key":"testKey1", "value":"987", }, { "key":"testKey2", "value":"123", } ] } ] } I am trying to update value for "key":"testKey2" I have such update statement: await coll

How to update deeply nested array with C# MongoDB.Driver?

三世轮回 提交于 2021-02-07 02:57:01
问题 I have such model: { "_id":"5b90eea8c02e062be2888446", "storeGuid":"e97d4730-9b8a-49ed-be87-caf4054439aa", "storeId":"0", "storeDbName":"0", "tenant":"dev", "configGroup":[ { "groupName":"peopleCounter", "config":[ { "key":"averageWaitPeriodTime", "value":"60", } ] }, { "groupName":"sessionMonitor", "config":[ { "key":"testKey1", "value":"987", }, { "key":"testKey2", "value":"123", } ] } ] } I am trying to update value for "key":"testKey2" I have such update statement: await coll

How to update deeply nested array with C# MongoDB.Driver?

倾然丶 夕夏残阳落幕 提交于 2021-02-07 02:56:14
问题 I have such model: { "_id":"5b90eea8c02e062be2888446", "storeGuid":"e97d4730-9b8a-49ed-be87-caf4054439aa", "storeId":"0", "storeDbName":"0", "tenant":"dev", "configGroup":[ { "groupName":"peopleCounter", "config":[ { "key":"averageWaitPeriodTime", "value":"60", } ] }, { "groupName":"sessionMonitor", "config":[ { "key":"testKey1", "value":"987", }, { "key":"testKey2", "value":"123", } ] } ] } I am trying to update value for "key":"testKey2" I have such update statement: await coll

MongoDB C# - how to save arbitrary JSON document as dynamic type?

为君一笑 提交于 2021-02-06 03:31:07
问题 I am trying to write a general purpose Web Api controller that will allow me to save a JSON document to a collection WITHOUT specifying a C# type. I've tried to condense the code down to the essentials: public class PassThroughController : ApiController { [Route("api/mongodb/{collection}")] public void Post(string collection, dynamic document) { const string connectionString = "mongodb://localhost"; var client = new MongoClient(connectionString); var db = client.GetServer().GetDatabase(

Properly using / handling DateTimeOffset in MongoDB

筅森魡賤 提交于 2021-02-04 14:48:05
问题 public class ScheduledEvent : Event { public DateTimeOffset StartDateTime { get; set; } } StartDateTime = 5/27/2013 2:09:00 AM +00:00 representing 05/26/2013 07:09 PM PST What's recorded in MongoDB: > db.ScheduledEvent.find().toArray() [ { "_id" : BinData(3,"ZE2p31dh00qb6kglsgHgAA=="), "Title" : "Today 26th at 7:09pm", "Length" : "00:00:00", "MoreInformation" : "http://1.com", "Speakers" : [ { "_id" : BinData(3,"ndzESsQGukmYGmMgKK0EqQ=="), "Name" : "Mathias Brandewinder" } ], "Location" : { "

Cannot use Mongo's Filter with DateTime and BsonString?

孤者浪人 提交于 2021-01-29 15:23:50
问题 I'm using C3 with mongo and I'm trying to query documents by dateCreated property. For example , if I extract the first record by : var models = await c.FindAsync(a=>true); var list = await models.ToListAsync(); list.First().Dump(); Then I see the result : Please notice that the value is BsonString , and that I have no entity behind it. The problem is that now , I want to query the collection based on datetime : So I did this : var filter = Builders<BsonDocument> .Filter .And ( Builders

Unable to connect to MongoDb (using authentication) using mongocsharpdriver 2.7.0

一曲冷凌霜 提交于 2021-01-29 14:13:23
问题 I have created a sample c# console application to connect to mongodb hosted on CentOS machine on a docker. The command used to create container is below: docker run -d --name mongodb-container -p 2020:27017 -v /home/mongodb_data:/var/lib/mongodb/data -v /home/mongodb_log:/var/log/mongodb -v /home/mongod.conf:/etc/mongod.conf -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret mongo System IP: 172.17.103.158 Mongodb docker port: 2020 Now coming to c# code class users