FAQ Database Discussion Community


Documents not expiring using TTL in mongodb 3.0 (.Net driver 2.0)

mongodb,mongodb-csharp,mongodb-csharp-2.0
I have the following code. The EnsureIndexes is called by the constructor to make sure that a TTL index has been created on the _expireAt field. Then when a document is inserted by calling the AddOrUpdateItem method, it adds a future date to the _expireAt field. However, that date passes...

Update all properties of object in MongoDb

c#,.net,mongodb,mongodb-csharp,mongodb-csharp-2.0
I'm using the MongoDB .Net driver in my project. I want to update all of the properties of my object that is stored in MongoDB. In the documentation, update is shown like this: var filter = Builders<BsonDocument>.Filter.Eq("i", 10); var update = Builders<BsonDocument>.Update.Set("i", 110); await collection.UpdateOneAsync(filter, update); But I don't want...

Upgrading IBsonSerializer with Mongo driver 2

c#,mongodb,mongodb-csharp-2.0
The old implementation of the Mongo Drivers was leading to this kind of code: public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType) { if (nominalType == typeof(T)) { if (typeof(V) == typeof(string)) return _deSerializeFunc(bsonReader.ReadString()); else if (typeof(V) == typeof(int)) return _deSerializeFunc(bsonReader.ReadInt32()); else if (typeof(V) == typeof(double)) return _deSerializeFunc(bsonReader.ReadDouble()); else if...

How to cast mongo collection to interface C#

c#,.net,mongodb,mongodb-csharp,mongodb-csharp-2.0
I have a repository with signature: public Task<IList<IFoo>> GetList() { } How do I cast mongoDb collection to this interface? (MongoDb Driver 2.0) public Task<IList<IFoo>> GetList() { Task<List<Foo>> foo = this.database.GetCollection<Foo>("Foo").Find(e => true).ToListAsync(); return foo ; // ?? somehow cast Task<List<Foo>> to Task<IList<IFoo>> } also, this code bothers me collection.Find(e...

Does MongoDB successful insert guarantee populated ID's?

c#,.net,mongodb-csharp,mongodb-csharp-2.0
We are using the MongoDB C# driver to insert a collection of records using InsertManyAsync. We currently have code that iterates the entire collection after InsertManyAsync returns, confirming that the ID's are populated. I would expect that the driver takes care of this already, but I have not found documentation...

MongoDB C# Driver 2.0 - Update document

c#,mongodb,mongodb-csharp,mongodb-csharp-2.0
I'm currently upgrading my code to MongoDB C# driver 2.0 and I'm having issues upgrading the code to update documents. using the old version I was able to do something like this: MyType myObject; // passed in var collection = _database.GetCollection<MyType>("myTypes"); var result = collection.Save(myObject); I'm struggling to find a...

Unwind then Group aggregation in MongoDB C#

c#,mongodb,mongodb-csharp,mongodb-csharp-2.0
I'm having some trouble with the new C# 2.0 MongoDB driver and the aggregation pipeline. Basically, I'm trying to return the most popular elements within an array field on the object. The field type is: IList<string> FavouritePlaceIds { get; set; }. I have the following MongoDB aggregation which is working...

Difference between Find and FindAsync

c#,mongodb,mongodb-query,mongodb-csharp-2.0
I am writing a very, very simple query which just gets a document from a collection according to its unique Id. After some frusteration (I am new to mongo and the async / await programming model), I figured this out: IMongoCollection<TModel> collection = // ... FindOptions<TModel> options = new FindOptions<TModel>...

Understanding the changes in Mongodb new c# driver (async and await)

c#,mongodb,mongodb-csharp,mongodb-csharp-2.0
The new c# driver is totally async and in my understanding twists a little bit the old design patterns such as DAL in n-tier architecture. In my mongo DALs I use to do: public T Insert(T entity){ _collection.Insert(entity); return entity; } this way I can get the persisted ObjectId. Today,...

Get generated script in MongoDB C# driver

c#,.net,mongodb,mongodb-csharp,mongodb-csharp-2.0
I am using MongoDB.Driver 2.0.0. Is there any way to see a generated script from linq to MongoDB? For example my query is like: IFindFluent<ProductMapping, ProductMapping> findFluent = Collection.Find( x => hashValues.Contains(x.UrlHash) && x.ProductTopic == topicId); How would this (or more complex queries) be represented in the MongoDB shell?...

How to do an upsert with MongoDB 2.0?

c#,mongodb,mongodb-csharp-2.0
The interface of MongoDB has completely changed from the previous one. Here you can see the official documentation with some examples about how to search, insert and update but what about upserts? Idea for meta: I've tried to search on google and on SO but many resources refer to the...

How do I search nested criteria using MongoDB c# driver (version 2)?

c#,linq,mongodb,mongodb-csharp,mongodb-csharp-2.0
I have a collection of documents that can contain criteria grouped into categories. The structure could look like this: { "Name": "MyDoc", "Criteria" : [ { "Category" : "Areas", "Values" : ["Front", "Left"] }, { "Category" : "Severity", "Values" : ["High"] } ] } The class I'm using to create...

MongoDB: How can I register conventions with Mongo 2.0

c#,mongodb,mongodb-csharp-2.0
I am initializing the MongoClient in this way: var client = new MongoClient(MongoConnectionString) But how can I explain it that it should use the convention classes I've defined? I've put them inside a class named BsonClassMap...

MongoDB .Net driver 2.0 Builders Filter (field to array comparison)

mongodb,mongodb-query,mongodb-csharp,mongodb-csharp-2.0
I need to get all usernames from "followingList.username" and compare with posts' usernames, if there any match need to add that one to an array. Person Model { "_id" : ObjectId("554f20f5c90d3c7ed42303e1"), "username" : "fatihyildizhan", "followingList" : [ { "_id" : ObjectId("55505b6ca515860cbcf7901d"), "username" : "gumusluk", "avatar" : "avatar.png" }, { "_id"...

get all documents from mongoDB collection

c#,.net,mongodb,mongodb-csharp,mongodb-csharp-2.0
I need to retrieve all the documents that are in my collection in MongoDB, but I cannot figure out how. I have declared my collection like this private static IMongoCollection<Project> SpeCollection = db.GetCollection<Project>("collection_Project"); and I followed what is explained in this MongoDB tutorial. I ajusted it for my needs, like:...

How to use MongoDB C# Driver without specifying a class

c#,mongodb,mongodb-csharp-2.0
I am using MongoDB c# driver 2.0. I a trying to get a collection without specifying a type or class. Observe: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Core; using MongoDB.Driver.Linq; using MongoDB.Shared; namespace Meshplex.Service.DataWarehouse { public class ProfileControllerMongoDB { private IMongoDatabase...

Query with filter builder on nested array using MongoDB C# driver

c#,mongodb,mongodb-query,mongodb-csharp,mongodb-csharp-2.0
Consider the following object structure stored as documents: public class Foo { public string Id { get; set; } public ICollection<FooBar> Bars { get; set; } // ... } public class FooBar { public string BarId { get; set; } // ... } Using a LINQ-style query with the driver...

Initialize MongoClient with MongoClientSettings in C#

c#,.net,mongodb,mongodb-csharp,mongodb-csharp-2.0
I'm trying to initialize the MongoClient from the Mongo 2.0 driver as follows: MongoClientSettings settings = new MongoClientSettings(); settings.WaitQueueSize = int.MaxValue; settings.WaitQueueuTimeout = new TimeSpan(0,2,0); settings.MinConnectionPoolSize = 1; settings.MaxConnectionPoolSize = 25; settings.Server = new MongoServerAddress("mongodb://localhost"); client = new MongoClient(settings) However, when I now try to insert a document with this...