mongodb-.net-driver

Mongodb aggregation query to subtract and grouping of cumulative value

℡╲_俬逩灬. 提交于 2019-11-29 07:14:41
{ "_id" : ObjectId("58f5a22d22679039176d2ee8"), "MachineID" : NumberInt("1001"), "Timestamp" : ISODate("2017-04-18T07:01:01.000+05:30"), "Utilization" : NumberInt("63654480"), "RunStatus" : NumberInt("1"), "ProductsCount" : NumberInt("681350") }, { "_id" : ObjectId("58f5a22d22679039176d2ee9"), "MachineID" : NumberInt("1001"), "Timestamp" : ISODate("2017-04-18T07:02:02.000+05:30"), "Utilization" : NumberInt("63655480"), "RunStatus" : NumberInt("1"), "ProductsCount" : NumberInt("681370") }, { "_id" : ObjectId("58f5a22d22679039176d2eea"), "MachineID" : NumberInt("1001"), "Timestamp" : ISODate(

How can I 'AND' multiple $elemMatch clauses with C# and MongoDB?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 07:03:12
I am using the 10Gen sanctioned c# driver for mongoDB for a c# application and for data browsing I am using Mongovue. Here are two sample document schemas: { "_id": { "$oid": "4ded270ab29e220de8935c7b" }, "Relationships": [ { "RelationshipType": "Person", "Attributes": { "FirstName": "Travis", "LastName": "Stafford" } }, { "RelationshipType": "Student", "Attributes": { "GradMonth": "", "GradYear": "", "Institution": "Test1", } }, { "RelationshipType": "Staff", "Attributes": { "Department": "LIS", "OfficeNumber": "12", "Institution": "Test2", } } ] }, { "_id": { "$oid":

Updating an embedded document in MongoDB with official C# driver

 ̄綄美尐妖づ 提交于 2019-11-29 03:55:22
问题 If I have a Company collection which contains embedded Divisions: { "_id": 1 "_t": "Company", "Name": "Test Company" "Divisions": [ { "_id": 1 "_t": "Division", "Name": "Test Division 1" }, { "_id": 2 "_t": "Division", "Name": "Test Division 2" } ] } What is the best way to save/update an entire Division when using the official 10gen C# driver? (The latest 0.9 release.) I'm using Update.AddToSetWrapped to add Divisions, and that works fine, but I'd also like to be able to update documents

C# MongoDB.Driver GetServer is Gone, What Now?

爱⌒轻易说出口 提交于 2019-11-29 03:51:21
From the mongoDB.Driver docs ( http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/ ) Get a Reference to a Server Object To get a reference to a server object from the client object, write this: var server = client.GetServer(); In the latest release the GetServer method is gone, but the doc have not been updated, what do we use now? Thanks for your time. GetServer is part of the old API. To use the new, shiny and async -ready API simply call GetDatabase directly on the client to get an IMongoDatabase and GetCollection on it to get an IMongoCollection : var db = client

How to insert data into a mongodb collection using the c# 2.0 driver?

☆樱花仙子☆ 提交于 2019-11-29 03:28:46
I'm using the MongoClient in my c# console application to connect to MongoDB https://github.com/mongodb/mongo-csharp-driver/releases/tag/v2.0.0-rc0 My code class Program { static void Main(string[] args) { const string connectionString = "mongodb://localhost:27017"; // Create a MongoClient object by using the connection string var client = new MongoClient(connectionString); //Use the MongoClient to access the server var database = client.GetDatabase("test"); var collection = database.GetCollection<Entity>("entities"); var entity = new Entity { Name = "Tom" }; collection.InsertOneAsync(entity);

How to remove one 'document' by 'ID' using the Official C# Driver for MongoDB?

匆匆过客 提交于 2019-11-29 02:57:33
Can someone please show me, if there is a better way to remove one document from MongoDB using the Official C# Driver than what I have below- var query = Query.EQ("_id", a.Id); database.GetCollection<Animal>("Animal").Remove(query); This code works, but seems too much work to me. The "Save" command for example- takes an instance and updates it. I want something like- Remove(item) . Remarks: I'm trying to use the official driver of C# rather than NoRM or Samus which seems out of date. That's the way you do it. I'm sure you know this, but if you want to put it on one line you could combine it so

Is there mongodb C# driver support System.Dynamic.DynamicObject in .NET 4?

烈酒焚心 提交于 2019-11-29 02:26:13
问题 Im working on a project that use .NET Razor and mongodb. I would like to do something like this: @{ var feeds = DP.Database.GetCollection("feeds").FindAll(); } <ul> @foreach (dynamic feed in feeds) { <li>@feed.message - @feed.from.name</li> } </ul> However, the current mongodb C# driver FindAll() return collection of BsonDocument which does not support dynamic object. Anybody know a .NET 4 dynamic supported mongodb C# driver? Thanks a lot 回答1: Currently, there is no support for dynamic in the

Creating MongoDB Unique Key with C#

本小妞迷上赌 提交于 2019-11-29 01:50:12
问题 I am fighting to create a unique field EmailAddress . I've already seen in forums that I have to create an index, but it didn't work out for me so far. Does anyone have a code example? Do I have to create the index on every save/call, or is it enough to create it only once? I tried this code: DB.GetCollection<User>(Dbname) .EnsureIndex(new IndexKeysBuilder() .Ascending("EmailAddress"), IndexOptions.SetUnique(true)); DB.GetCollection<User>(Dbname).Save(user, SafeMode.True); My User model looks

C# mongo queries with json strings

人盡茶涼 提交于 2019-11-28 21:36:20
This seems so basic that I'm sure I've just overlooked a class or a method somewhere, but for the life of me, I can't find it. I've got a json string like so: { SendId: 4, "Events.Code" : { $all : [2], $nin : [3] } } I can run this in the mongo shell against a find() or a count() and get what I'm looking for. What is the easiest way to deal with this in C#? Here's what I've found: The methods I'm finding are all wanting an IMongoQuery , which is just a marker interface BsonDocument has a nice Parse method, but it doesn't implement IMongoQuery QueryDocument inherits from BsonDocument , and it

Translate FilterDefinition<TDocument> to regular json mongo query that i can run in a mongo shell

耗尽温柔 提交于 2019-11-28 20:37:49
I have many complex queries that I sometimes wish to check directly against Mongo for debugging \ explaining() purposes. With the newer 2.0+ c# driver, i'm not sure how to do this. With the previous version there was a thing called IMongoQuery and This worked. A simple example: FilterDefinition<LalalaEvent> filter = Builders<LalalaEvent>.Filter .Where(e => ids.Contains(e.Id) && e.Deleted != true ); If you're using the latest version of the driver, which is 2.0.1 you can easily put that filter in a Find operation, get back an IFindFluent and print its ToString : var filter = Builders