mongodb-.net-driver

How to call a stored JavaScript in MongoDb from C#

非 Y 不嫁゛ 提交于 2019-12-02 00:48:48
问题 I'm evaluating the porting of SQL Server database to MongoDb. The problem is moving stored procedures, I read about MongoDb stored JavaScript and I would like make some test in in .Net. I've installed MongoDb driver 2.4.0 and created this function on MongoDb named test_function : function (x) { return x; } This is the code I use to try to invoke that function: MongoClient oMongoClient = new MongoClient(Properties.Settings.Default.MongoCN); IMongoDatabase oMongoDatabase = oMongoClient

Update Embedded document in mongodb using C#

陌路散爱 提交于 2019-12-01 23:43:38
Suppose you have the next class. It contains the systems in which the agent has worked public class AgentHistory { public ObjectId Id { get; set; } public Guid SystemId { get; set; } public Guid CampaignId { get; set; } public List<Agent> Agents { get; set; } } Now when I get a new agent I do the next thing: public override AgentHistory Save(AgentHistory agent) { if (agent == null) throw new ArgumentNullException("agent"); if (_repository.Exists(agent)) { AgentHistory dbEntity = _repository.FindById(agent.SystemId, agent.CampaignId); dbEntity.Agents.AddRange(agent.Agents); _repository

Call MongoDB Stored Javascript Function in C# .Net

江枫思渺然 提交于 2019-12-01 23:12:21
问题 I need to call a MongoDB Stored JavaScript Function in C# Code. My Stored JavaScript Function GetUserInfo function() { return db.getCollection('Profession').find({}); } Execution : db.loadServerScripts(); GetUserInfo(); It returns the Collection with following documents (Here I pasted only 2 Documents, in real I'm having more than 10K Documents) { "_id" : ObjectId("575845a713d284da0ac2ee81"), "Profession_id" : "575841b313d284da0ac2ee7d", "Prof_Name" : "Chief Officer" } { "_id" : ObjectId(

Having Difficulty Using MongoDb C# Driver's Sample()

拟墨画扇 提交于 2019-12-01 22:53:06
I am trying to get some random items from the database using the Sample() method. I updated to the latest version of the driver and copied the using statements from the linked example. However, something isn't working, and I am hoping it's some simple mistake on my part. All the relevant information is in the image below: Edit: Greg, I read the aggregation docs here and the raw db method doc here , but I still don't get it. Last two lines are my attempts, I have never used aggregation before: var mongoCollection = GetMongoCollection<BsonDocument>(collectionName); long[] locationIds = new long[

Call MongoDB Stored Javascript Function in C# .Net

可紊 提交于 2019-12-01 21:14:02
I need to call a MongoDB Stored JavaScript Function in C# Code. My Stored JavaScript Function GetUserInfo function() { return db.getCollection('Profession').find({}); } Execution : db.loadServerScripts(); GetUserInfo(); It returns the Collection with following documents (Here I pasted only 2 Documents, in real I'm having more than 10K Documents) { "_id" : ObjectId("575845a713d284da0ac2ee81"), "Profession_id" : "575841b313d284da0ac2ee7d", "Prof_Name" : "Chief Officer" } { "_id" : ObjectId("575845d213d284da0ac2ee82"), "Profession_id" : "575841b313d284da0ac2ee7d", "Prof_Name" : "Executive Officer

MongoDB c# driver - Can a field called Id not be Id?

喜欢而已 提交于 2019-12-01 18:33:39
问题 More particulary, there is a class class X { .... string Id { get; set; } } class Y : X { ObjectId MyId { get; set; } } I would like MyId to be an id for Y, i.e. to be mapped to _id. Is it possible? I get an exception after this code: var ys = database.GetCollection("ys"); ys.InsertBatch(new Y[] { new Y(), new Y()}); The exception: {"Member 'MyId' of class 'MongoTest1.Y' cannot use element name '_id' because it is already being used by member 'Id'."} Full test case: using System; using System

MongoDB c# driver - Can a field called Id not be Id?

烈酒焚心 提交于 2019-12-01 18:18:52
More particulary, there is a class class X { .... string Id { get; set; } } class Y : X { ObjectId MyId { get; set; } } I would like MyId to be an id for Y, i.e. to be mapped to _id. Is it possible? I get an exception after this code: var ys = database.GetCollection("ys"); ys.InsertBatch(new Y[] { new Y(), new Y()}); The exception: {"Member 'MyId' of class 'MongoTest1.Y' cannot use element name '_id' because it is already being used by member 'Id'."} Full test case: using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB.Bson; using MongoDB.Bson

MongoDB Driver 2.0 C# is there a way to find out if the server is down? In the new driver how do we run the Ping command?

好久不见. 提交于 2019-12-01 17:41:20
问题 How do you call the Ping command with the new C# driver 2.0? In the old driver it was available via Server.Ping() ? Also, Is there a way to find out if the server is running/responding without running the actual query? Using mongoClient.Cluster.Description.State doesn't help because it still gave the disconnected state even after the mongo server started responding. 回答1: You can check the cluster's status using its Description property: var state = _client.Cluster.Description.State If you

MongoDB Driver 2.0 C# is there a way to find out if the server is down? In the new driver how do we run the Ping command?

元气小坏坏 提交于 2019-12-01 17:27:42
How do you call the Ping command with the new C# driver 2.0? In the old driver it was available via Server.Ping() ? Also, Is there a way to find out if the server is running/responding without running the actual query? Using mongoClient.Cluster.Description.State doesn't help because it still gave the disconnected state even after the mongo server started responding. You can check the cluster's status using its Description property: var state = _client.Cluster.Description.State If you want a specific server out of that cluster you can use the Servers property: var state = _client.Cluster

How can you debug stored javascript functions in MongoDB?

狂风中的少年 提交于 2019-12-01 17:22:03
I'm thinking of moving some workflow logic from C# code to stored JS in MongoDB (for example, wen a user sends a message, a bunch or records is to be created in different collections, which right now I do in C#), but I'm concerned whether I would be able to debug that JS code if things don't work correctly. There isn't a particular facility for that. One thing you could do is run some of that code in the mongo shell, which can execute exactly the same javascript as the server. The shell doesn't have a debugger but with its interactive prompt it would be much easier to try things, inspect