Questions tagged [mongodb-.net-driver]

7

votes
1

answer
131

Views

BsonDocument to dynamic Expression or in-memory caching of results

I am trying to write a proxy class for IMongoCollection so that I can use an in-memory cache for some of the method implementations. The issue, however, is that almost all the filters are of type FilterDefinition which means we can call Render on them to get a BsonDocument. I am wondering if there a...
Node.JS
1

votes
0

answer
6

Views

How to create index based on nested array element in C# Mongodb strongly typed driver

This question is quite on the same principles as this one but I'd like to create an index using strongly typed approach on an object property when this object is nested in an array of the collection. I can use: new CreateIndexModel( Builders.IndexKeys.Ascending( a ) ) where a is an Expression which...
lemon
1

votes
1

answer
956

Views

Using Facets in the Aggregation Framework C#

I would like to create an Aggregation on my data to get the total amount of counts for specific tags for a collection of books in my .Net application. I have the following Book class. public class Book { public string Id { get; set; } public string Name { get; set; } [BsonDictionaryOptions(Dictiona...
Adzy_12
1

votes
1

answer
1.8k

Views

C# MongoDB - How to add and remove item to multiple nested array elements?

I have a document in MongoDB that looks like that: { 'Id':'123', 'Product': 'test', 'Tags':[ { 'Name': 'name', 'Categories': [ { //item }, { //item } ] }, { 'Name': 'name', 'Categories': [ { //item }, { //item } ] } ] } Now, I need to add a new Item and it needs to be added to all of the categories...
Liran Friedman
1

votes
1

answer
1.3k

Views

c# Mongodb 2.0 driver get UpdateManyAsync result

Is there any way to get the updated collections after/before the update? What I can think of is pretty heavy. get what i want to update using filter. update everything i got in the previous state get all by ids again and check what has been updated. Why I need this? I have a service which constantly...
Ori Refael
1

votes
2

answer
519

Views

MongoDB 3.2 C# driver version 2.2.3.3 Gridfs Download large files more than 2gb

I am uploading files using the following code: using (var s = File.OpenRead(@'C:\2gbDataTest.zip')) { var t = Task.Run(() => { return fs.UploadFromStreamAsync('2gbDataTest.zip', s); }); return t.Result; } //works for the files below 2gb var t1 = fs.DownloadAsBytesAsync(id); Task.WaitAll(t...
Raj
1

votes
2

answer
237

Views

Deserializing a C# Dictionary in MongoDB Returns Null

I have a simple class: public class Foo { public ObjectId Id { get; set; } public Dictionary Bar { get; set; } } Nothing else, no custom mappings, etc. When I insert it into MongoDB, it works perfectly, but the Bar property is ignored, although, at insertion time, it is not null. I tried adding [Bso...
Ricardo Peres
1

votes
1

answer
179

Views

mongodb .net driver V2: How to do array operations

I'm totally lost on doing CRUD and other operations on array elements in an embedded Array in mongodb using c# drivers. given I have the following classes (simple example): public class Child { public ObjectId Id; public DateTime dateOfBirth; public string givenName; } class Family { public ObjectId...
Thomas
1

votes
1

answer
72

Views

How to fetch first 1000 documents that don't have certain field?

Say, I have collection People. How should I fetch first 1000 documents that doesn't have a field Phone? As I understand, I should use $exists however I cannot understand how to use it from .NET driver and there is next to no info on that topic on the internet. Any help will be appreciated. Thanks!
nicks
1

votes
1

answer
1.3k

Views

Update a nested Array in Mongo with c#

I have a document like this { '_id': '63dafa72f21d48312d8ca405', 'tasks': [{ '_ref': '63d8d8d01beb0b606314e322', 'data': { 'values': [{ 'key': 'Deadline', 'value': '2014-10-13' }] } }, { '_ref': '84dd046c6695e32322d842f5', 'data': { 'values': [] } }] } Now I want to update the value inside values wh...
Kleisophabo
1

votes
1

answer
79

Views

Type argument 'MongoDB.Bson.ObjectId' violates the constraint of type parameter 'TTarget'

I am trying to build a MVC website with MongoDB. I am a newbie with MongoDB. When I try to insert a new data into a collection, it throws the error below Type argument 'MongoDB.Bson.ObjectId' violates the constraint of type parameter 'TTarget'. My code for inserting like below... public void Add(T i...
Arif YILMAZ
1

votes
1

answer
533

Views

Project a single array item mongodb C#

I have the following json { 'name':'Student', 'Classes' : ['Chemistry','Math','Algebra'] } and the following poco public class Studen { public string Name {get;set;} public string[] Classes {get; set;} } I want to query a specific student that takes a specific class (say Math), and the poco to have...
Pacman
1

votes
1

answer
1k

Views

Async update or insert MongoDB documents using .Net driver

I have the document like this public class SomeDocument { public Guid Id { get; set; } public string PropertyA { get; set; } public string PropertyB { get; set; } } Now I have two different services (A and B) that update PropertyA and PropertyB appropriately and work in asynchronous manner. That me...
Mr. Pumpkin
1

votes
1

answer
674

Views

Map projection result with mongodb C# driver

I need to map to a simplified document some documents in a collection. I can obtain what I need with this in the mongo shell: db.getCollection('items').aggregate([ { '$project': { 'Team': '$TeamId', 'Marker': '$Properties.marker.Value' }} ]) I need to obtain the same result with C# driver (version 2...
Daniele Armanasco
1

votes
1

answer
709

Views

Insert into nested array in C# MongoDB

I have the following document in a collection called Users in MongoDB. I am trying to insert a into beersInCellar[] of the mycellar cellar. I've tried all kinds of combinations in the filter and update objects but just can't wrap my head around inserting into a nested array. I know how to find a...
Jrow
1

votes
1

answer
388

Views

How to optimize C# mongodb query on large datadases?

I have a database table having 100 million records. Screen Shot is taken from Robomongo Table Schema: There are 100 million records When I run the following code. I get results, but It takes around 1 minute to get completed. I need to optimize the query to get results faster. What I have done till n...
Umer
1

votes
1

answer
100

Views

C# Driver- LINQ - Aggragate - Contains in Int[]

Mongo Driver Version: 2.2.4.26 I have the following query that performs some aggregations. If I remove the Contains predicate from my Where clause, the query executes successfully. I would like to reduce and perform the aggregation on the server before realizing the data into the client memory. The...
Lui_B
1

votes
1

answer
48

Views

Application specific exception wrapping “MongoDuplicateKeyException” not catch'ed

Out of need have created application exception which wraps a MongoDuplicateKeyException and throwing that exception like below Public class AppException : Exception { // all constructor implementation public int ErrorCode { get; set; } public string AppMessage { get; set; } } In method catching and...
Rahul
1

votes
2

answer
640

Views

MongoDB: Using $sample with C# driver

I'm trying to express the following query using the MongoDB C# driver (2.4.4): db.media.aggregate({ $sample: { size: 1 }}) This what I have so far: BsonDocument sample = new BsonDocument { { '$sample', new BsonDocument { { 'size', 1 } } } }; MongoBlob mongoBlob = await _collection .Aggregate() .Grou...
Roman Klimenko
1

votes
1

answer
259

Views

How to delete an element inside array of a nested document in C# Mongodb strongly typed driver

I am using the official C# MongoDb strongly typed driver version 2.5.0 to interact with MongoDB. Consider the following classes: public class Author { public Author() { } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } public string FirstName { get; set; } public stri...
Waxren
1

votes
1

answer
27

Views

How do I find out which pipelines have been used for creating a view?

What is the shell command for finding out which pipelines have been used for creating a certain view? MongoDB version is 3.6.4 Also, is there a way of getting the pipelines with the .net driver, version 2.6.1?
Hackworth
1

votes
1

answer
249

Views

How to sort by condition using the mongo driver in c#

I've been following the mongo driver (2.4) documentation and am currently stuck trying to put a conditional on a sort. Following the documentation, I got a AsQueryable instance and used the where function with an expression passed in. That works fine until the moment I want to order by a conditional...
Kelsey Abreu
1

votes
1

answer
33

Views

Can't sum up all Int32 field values

I've got a mongo db collection with documents like this All I need is to count the total sum of the 'debt' fields of all documents. But I have no idea of how to make it. The official docs seem to be too obscure and don't help at all because there's absolutely no information on how to use C# driver...
Konstantin
1

votes
2

answer
982

Views

MongoDB advanced query

Let's say I have a Collection with two properties: amount and amountreceived that are decimals. I want to write a query that will return items from the collection that meet the criteria of the amount being greater than the amount received. Currently I have a javascript function that looks like this:...
strickland
1

votes
1

answer
8.9k

Views

MongoDB/C# get all values of a certain key in all documents

I have a MongoDB collection called 'employees' where there are documents like this: { '_id': '4dc077b07701540f34000001', 'FullName': 'Fullname...', 'FirstName': 'First name...', 'LastName': 'Last name...', 'Title': 'Title... ', 'Location': 'Location...', 'Customer': 'Customer...', 'Phone': 'P...
bleakgadfly
1

votes
1

answer
800

Views

Partial mongodb upsert using the c# driver?

Mongo version 1.8.2. Assume I have a class like public class Acc { public int _id { get; set; } public int? Foo { get; set; } public int? Bar{ get; set; } } Acc a = new Acc { _id = 1, Foo = 3 }; I'd like to call myCollection.Save(a), such that if it doesn't exist, its inserted (easy so far) i...
Nik
1

votes
1

answer
1.5k

Views

Creating generic MongoDb helper class in C# 4.0

I am trying to create a helper class for interacting with MongoDb in C# 4.0. I've been reading some of the documentation on serializing to Bson, etc. but am a little lost. What I have is a generic MongoHelper class with and Add(T objectToAdd), Delete(T objectToDelete) and Update(T objectToUpdate) m...
RockyMountainHigh
1

votes
5

answer
4.4k

Views

Mongo Db records retrieval very slow using c# api

I am trying to retrieve 100000 docouments from MongoDb like below and its taking very long to return collection. var query = Query.EQ('Status', 'E'); var items = collection.Find(query).SetLimit(100000).ToList(); Or var query = Query.GT('_id', idValue); var items = collection.Find(query).SetLimit(100...
Venkat
1

votes
1

answer
2.3k

Views

How to update nth element inside an array without $pull or $pop?

How to update value of the nth position element inside an array? I want to update index 2 of category_id. One solution is ($pop or $pull) and $addToSet. Is there any other solution? Array ( [_id] => MongoId Object ( [$id] => 4f93ae990c211da128000004 ) [name] => UnderWater Photography Tips [categor...
P K
1

votes
2

answer
5.6k

Views

Updating a single element in a nested list with official C# MongoDB driver

I have a simple game with multiple rounds and I want to update the most recent round: class Game { public ObjectId Id { get; set; } public List Rounds { get; set; } } class Round { public int A { get; set; } public int B { get; set; } } How can I do the equivalent of games.Rounds.Last().A = x using...
Wesley Tansey
1

votes
1

answer
1.8k

Views

Insert in to a nested array Using C# Official Driver MongoDB

Here is my exact schema: { '_id' : ObjectId('4fb4fd04b748611ca8da0d48'), 'Name' : 'Categories', 'categories' : [{ '_id' : ObjectId('4fb4fd04b748611ca8da0d46'), 'name' : 'Naming_Conventions', 'sub-categories' : [{ '_id' : ObjectId('4fb4fd04b748611ca8da0d47'), 'name' : 'Namespace_Naming', 'standards'...
John Stewart
1

votes
1

answer
1.2k

Views

MongoDB C# Query for 'NotLike' on string

I'm using official Mongo C# driver. As suggested in answer to one question I'm using the following for the 'like' operator - Query.Matches('name', 'Joe'); My question is how can I achieve the 'NotLike' functionality ?
BlitzKrieg
1

votes
2

answer
399

Views

Serializing a composite id into binary

I have the following test class that represents a composite _id: private sealed class Id { public int p0 { get; set; } public int p1 { get; set; } public int p2 { get; set; } public int p3 { get; set; } public int p4 { get; set; } public int p5 { get; set; } public int p6 { get; set; } public int p7...
IamIC
1

votes
2

answer
1.3k

Views

Insert documents into MongoDB only if all fields are unique

I have the following class structure: public class DBTestItem { public int p0; public int p1; public int p2; public int p3; public int p4; public int p5; public int p6; public int p7; public string data; } public sealed class MongoTestItem : DBTestItem { public ObjectId _id; } _id is meaningless to...
IamIC
1

votes
3

answer
1.2k

Views

MongoDB: MVC not serializing BsonId on post

I can't get the ObjectId for a model to return back with a value using the MongoDB csharp official library. When I submit the form that posts back to the controller, the PageID always comes back {000000000000000000000000} in the model. The HTML rendered has the valid id, that one field never comes...
uadrive
1

votes
0

answer
4

Views

MongoDb c# mapping recursive object with bsonextraelements

I have following classes: public class Property { public string Description { get; set; } public object Value { get; set; } [BsonExtraElements] public IDictionary OtherData { get; set; } } public class Device { public string DeviceId { get; set; } public IDictionary Properties { get; set; } } What...
Jakub Dropia
5

votes
0

answer
30

Views

Can't Connect MongoDB With SSL in Azure machine with another MVC application in Azure

I'm having problem in connecting MongoDB which is configured using SSL. I have MongoDB enterprise server in Azure virtual machine which has the following configuration. net: bindIp: 0.0.0.0 port: 27017 ssl: CAFile: 'C:\openssl-0.9.8h-1-bin\bin\rCA.pem' PEMKeyFile: 'C:\openssl-0.9.8h-1-bin\bin\rser.p...
Mani Kkr
1

votes
1

answer
676

Views

C# - MongoDB how to remove an item from multiple nested arrays by element value?

I have this JSON structure in a mongo db collection: { 'Id':'123', 'Product': 'test', 'Tags':[ { 'Name': 'name', 'Categories': [ { 'Name': 'test', 'OtherValue': ... } ] }, { 'Name': 'name', 'Categories': [ { 'Name': 'test', 'OtherValue': ... } ] } ] } Is there a way to be able to remove an item from...
Liran Friedman
1

votes
3

answer
1.3k

Views

Float inaccurate rounding in Mongodb with CS driver

When I save the float number into MongoDB using csharp driver it is not saved accurately. If my number is 1504.57 I expect the database will have the same number but for some reason it become 1504.56994628906 (with Double type in MongoDB). What's going on? How to keep data accurately? My object keep...
mimic
1

votes
2

answer
2.2k

Views

MongoDB transaction

I have a method like below in C#: private void Save(object) { mongoCollection.Save(object); someotherRelationaldb.Save(object); } I have two DBs in which I have to save a object. One is MongoDB and another is a relational DB SQL server. If the Commit on Relational DB fails, I want to rollback the Mo...
ajp

View additional questions