mongodb-.net-driver

Execute mongodb shell script via C# driver

风流意气都作罢 提交于 2019-11-28 10:12:17
I have read this question and haven't understand. Is there ability to execute arbitrary mongodb shell script via C# driver? var mongoServer = MongoServer.Create("mongodb://<connectionstring>"); var database = mongoServer.GetDatabase("mydatabase"); string mycollectionCount database.Eval("function() { return db.mycollection.count(); }").ToString(); This is useful when you are trying to change property types for example like this: string updateScript = @" function () { db.some_items.find().forEach(function(documentItem) { documentItem.some_collection.forEach(function(collectionItem) { if (typeof

How to query a sub document collection using MongoDB and C# driver

一曲冷凌霜 提交于 2019-11-28 08:20:57
问题 I have the following structure: public class ThreadDocument { public ThreadDocument() { Messages = new List<Message>(); Recipients = new List<Recipient>(); } [JsonIgnore] public ObjectId Id { get; set; } public IList<Recipient> Recipients { get; set; } public IList<Message> Messages { get; set; } public DateTime LastMessageSent { get; set; } public string LastSentByUserName { get; set; } public string LastSentAvatarUrl { get; set; } public string Snippet { get; set; } public int MessageCount

BsonSerializationException when serializing a Dictionary<DateTime,T> to BSON

心已入冬 提交于 2019-11-28 08:19:58
I've recently moved to the new MongoDB C# driver v2.0 from the deprecated v1.9 . Now, when I serialize a class that has a dictionary I sometimes run into the following BsonSerializationException : MongoDB.Bson.BsonSerializationException: When using DictionaryRepresentation.Document key values must serialize as strings. Here's a minimal reproduce: class Hamster { public ObjectId Id { get; private set; } public Dictionary<DateTime,int> Dictionary { get; private set; } public Hamster() { Id = ObjectId.GenerateNewId(); Dictionary = new Dictionary<DateTime, int>(); Dictionary[DateTime.UtcNow] = 0;

MongoDB C# Query for 'Like' on string

让人想犯罪 __ 提交于 2019-11-28 08:09:20
i am using official mongodb c# driver. i want to query mongodb simliar to SQL Like something like db.users.find({name:/Joe/} in c# driver c# query will looks like: Query.Matches("name", BsonRegularExpression.Create(new Regex("Joe"))); Update: As per @RoberStam suggestion, there is more simple way to do this: Query.Matches("name", "Joe") Sridhar For the c# driver 2.1 (MongoDB 3.0) var collection = database.GetCollection<BsonDocument>("<<name of the collection>>"); var filter = Builders<BsonDocument>.Filter.Regex("name", new BsonRegularExpression("Joe")); var result = await collection.Find

Query MongoDB Using 'ObjectId'

♀尐吖头ヾ 提交于 2019-11-28 08:06:59
I have inserted document s into MongoDB without an id . And I want to retrieve them by searching through their MongoDB ObjectId , that has been assigned in default . Here is my attempt- var query_id = Query.EQ("_id", "50ed4e7d5baffd13a44d0153"); var entity = dbCollection.FindOne(query_id); return entity.ToString(); And I get following error- A first chance exception of type 'System.NullReferenceException' occurred What is the problem? You need to create an instance of ObjectId and then query using that instance, otherwise your query compares ObjectId s to string and fails to find matching

How do I log my queries in MongoDB C# Driver 2.0?

若如初见. 提交于 2019-11-28 07:58:27
问题 Just upgraded my application to the latest stable MongoDB C# Driver 2.0. During the migration, basic functionality has been broken and even the simplest query like: this.collection.Find(e => e.Id == id).SingleOrDefaultAsync() doesn't return the correct data. Checked the class mappings and conventions but I would like to see the output query in order to properly identify the issue. So, how should it be done on the MongoClient side? Setting profiling on the database level is possible but not a

How do you update multiple field using Update.Set in MongoDB using official c# driver?

妖精的绣舞 提交于 2019-11-28 07:09:55
The following code will allow me to update the Email where FirstName = "john" and LastName = "Doe". How do you update both Email and Phone without using Save() method? MongoDB.Driver.MongoServer _server = MongoDB.Driver.MongoServer.Create("mongodb://localhost"); MongoDB.Driver.MongoDatabase _dataBase = _server.GetDatabase("test"); MongoDB.Driver.MongoCollection<Person> _person = _dataBase.GetCollection<Person>("person"); //Creat new person and insert it into collection ObjectId newId = ObjectId.GenerateNewId(); Person newPerson = new Person(); newPerson.Id = newId.ToString(); newPerson

How to save date properly?

限于喜欢 提交于 2019-11-28 06:49:58
I'm trying to save date (using C# official driver): val = DateTime.Parse(value).Date; //Here date is {11/11/2011 12:00:00 AM} var update = Update.Set("Date", val); ... When I select Date from the database, the value is {11/10/2011 10:00:00 PM} How to save only the date I want? Andrew Orsich c# driver by default (without extra settings) saving local dates as utc date into database (date - time zone offset) but reading back without any action (so, utc date). Because of this when you loading datetime from database you receive diff in 2 hours (your timezone offset). There are two approaches how to

C# mongodb - how to update nested array elements

懵懂的女人 提交于 2019-11-28 06:33:15
问题 I have the following JSON structure that represents an item { Id: "a", Array1: [{ Id: "b", Array2: [{ Id: "c", Array3: [ {...} ] }] }] } I need to be able to either replace the array element in Array2 with a new item or to replace just Array3 with a new array. Here is my code to replace the array item in Array2 : await Collection.UpdateOneAsync( item => item.Id.Equals("a") && item.Array1.Any(a => a.Id.Equals("b")) && item.Array1[-1].Array2.Any(b => b.Id.Equals("c")), Builders<Item>.Update.Set

MongoDB best practice for referencing

自闭症网瘾萝莉.ら 提交于 2019-11-28 05:56:40
I'm wondering what the best practice for modelling by using references would be given situation under. I'm using MongoRepository library. public class User : Entity { publis string Id { get; set; } public string Email { get; set; } public string Password { get; set; } } public class Post : Entity { public string Id { get; set; } public string Title { get; set; } public string Summary { get; set; } public DateTime Added { get; set; } public User Owner { get; set; } } When storing the Post I want only reference to Owner (User) object instead of whole object underlying. Currently I'm doing it