mongodb-.net-driver

How can I upsert a record and array element at the same time?

て烟熏妆下的殇ゞ 提交于 2019-12-02 07:15:23
That is meant to be read as a dual upsert operation, upsert the document then the array element. So MongoDB is a denormalized store for me (we're event sourced) and one of the things I'm trying to deal with is the concurrent nature of that. The problem is this: Events can come in out of order, so each update to the database need to be an upsert. I need to be able to not only upsert the parent document but an element in an array property of that document. For example: If the document doesn't exist, create it. All events in this stream have the document's ID but only part of the information

InsertMany Document in a MongoDB Collection using C# BsonArray

我怕爱的太早我们不能终老 提交于 2019-12-02 07:13:36
How to Insert more than one document in a Single statement using InsertMany() MongoDB Method in C# My MongoDB Database and Connections IMongoClient _client; IMongoDatabase _database; _client = new MongoClient(); _database = _client.GetDatabase("test"); var collection = _database.GetCollection<BsonDocument>("EmpInfo"); I'm having a Collection - BsonArray var EmpInfoArray = new BsonArray { new BsonDocument { {"EmpID", "100"}, {"EmpName", "John"}, {"EmpMobile", new BsonArray { new BsonDocument { {"MobNumber", "55566610"}, {"IsPreferred", true}, {"IsLive", false} }, new BsonDocument { {"MobNumber"

Duplicate a mongodb collection

99封情书 提交于 2019-12-02 06:53:30
What is a proper way to duplicate a collection in Mongodb on the same server using C#? MongoVUE has an option 'Duplicate collection', is there something similar for C#? There isn't a built-in way to copy collections with the C# driver, but you can still do it pretty simply as: var source = db.GetCollection("test"); var dest = db.GetCollection("testcopy"); dest.InsertBatch(source.FindAll()); Note, however, that this won't copy any indexes from the source collection. The shell's copyTo method has the same limitation so it's likely implemented similarly. I had the exact same problem, but while

How can I upsert a record and array element at the same time?

大城市里の小女人 提交于 2019-12-02 06:38:51
问题 That is meant to be read as a dual upsert operation, upsert the document then the array element. So MongoDB is a denormalized store for me (we're event sourced) and one of the things I'm trying to deal with is the concurrent nature of that. The problem is this: Events can come in out of order, so each update to the database need to be an upsert. I need to be able to not only upsert the parent document but an element in an array property of that document. For example: If the document doesn't

How to implement MongoDB nested $elemMatch Query in C#

十年热恋 提交于 2019-12-02 06:04:39
问题 I have a MongoDB collection in the following format. { "_id" : ObjectId("56c6f03ffd07dc1de805e84f"), "Details" : { "a" : [ [ { "DeviceID" : "log0", "DeviceName" : "Dev0" }, { "DeviceID" : "log1", "DeviceName" : "Dev1" } ], [ { "DeviceID" : "Model0", "DeviceName" : "ModelName0" }, { "DeviceID" : "Model1", "DeviceName" : "ModelName1" } ] ] } } And I am trying to fetch all the documents where the DeviceName in array "a" contains a particular value, say "Name0". However I could get the desired

How to have a different name for entries of a mongodb document in C#?

筅森魡賤 提交于 2019-12-02 05:48:40
I'm trying to do CRUD operations on documents in MongoDB and C# . I would like to have fixed structured domain entities in C# with long meaningful property names but since the name of each property will be saved in MongoDB for each document, that's not a good idea. That's because property names will be redundantly saved in database and affect the total storage and performance. The solution I can think of to overcome this problem, is to use different names for properties in C# than in MongoDB which means a mapping between them is needed. One elegant way of doing this is to incorporate C#'s

Dealing with how MongoDB stores DateTime when used with Service Locator Pattern

孤街浪徒 提交于 2019-12-02 04:26:58
问题 My colleague and I are at an impasse in a debate and other's input would be greatly appreciated. We utilize the Service Locator Pattern and a common interface to abstract all of our data access so we can easily swap between different data sources as our needs change. Our calling code has no indication of where the data is being stored or how. It simply accesses the data via the service it is fed from the service registry. The issue we are debating occurs when we have DateTime fields on an

Slice with Projection with C#

折月煮酒 提交于 2019-12-02 04:26:23
Is there any way to implement a slice along with a projection in just one query using the c# driver? Below is what i am trying to achieve using c#, but im stuck, can anyone help me out wit it? db.employee.find({"employeeId": "999"}, { "empActivity" : { "$slice": -1 } }, {"employeeId": 1, "empActivity.transId": 1, _id: 0}) Note : empActivity is a an array containing nested documents, my query above works perfectly via the mongo shell but i am not able to figure out its equivalent in C#. There is a way to do this with the C# driver. Methods can be chanined on builders, so all of .Slice() and

Update Embedded document in mongodb using C#

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 04:06:52
问题 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

Querying a subfield in documentdb

久未见 提交于 2019-12-02 02:38:57
问题 For example I have a document below for collection = delivery: { "doc": [ { "docid": "15", "deliverynum": "123", "text": "txxxxxx", "date": "2019-07-18T12:37:58Z" }, { "docid": "16", "deliverynum": "456", "text": "txxxxxx", "date": "2019-07-18T12:37:58Z" }, { "docid": "17", "deliverynum": "999", "text": "txxxxxx", "date": "2019-07-18T12:37:58Z" } ], "id": "123", "cancelled": false } is it possible to do a search with "deliverynum" = 999 and the output would be like below? { "doc": [ { "docid"