mongodb-.net-driver

Embedded MongoDB Document not getting an ID on save with C# Driver

假如想象 提交于 2019-12-10 15:55:35
问题 When I have a root document that has a list of documents and I save/insert, the root document always gets an ID from MongoDB, but the documents in the list don't, they remain the same as ObjectId.Empty . I'm not sure if I'm doing something wrong or if this is intended behavior? Here's an example of what I'm trying to do: Given the classes: public class Foo { public ObjectId Id { get; set; } public string Name { get; set; } public IList<Bar> Bars { get; set; } } public class Bar { public

Is it Ok to use mongoDB passwordless?

a 夏天 提交于 2019-12-10 15:43:44
问题 I am planning to install mongodb and the windows service which is connecting to it to the same machine. That machine will be in isolated network. When we do it like that. Is it ok to connect local mongodb passwordless? My planned properties will be like this... private MongoDatabase _db; public MongoDatabase DB { get { if (_db == null) { var mongoServer = MongoServer.Create(); _db = mongoServer.GetDatabase("myStatistics"); } return _db; } } private MongoCollection _collection; public

Is there a way to retrieve data from MongoDB without the _id field?

空扰寡人 提交于 2019-12-10 15:14:31
问题 I am using MongoDB in our project and I'm currently learning how things work. When inserting records, MongoDB auto generates the ID. When querying data, it always returns _id. This creates a problem when we deserialise the Bson/Json to out objects as our types don't have that field (and we don't really want to put it in). Does anyone know how to retrieve data without getting _id in the result? 回答1: Yes, just explicitly omit it: collection.Find(criteria) .SetFields(Fields.Include("oneField",

MongoDb SafeMode compare to WriteConcern

巧了我就是萌 提交于 2019-12-10 14:54:52
问题 Could anyone say how to replace obsolete SafeMode with WriteConcern ? In particular I am interesting in SafeMode.True ? Thanks. 回答1: SafeMode.True translates to WriteConcern.Acknowledged . From the driver source code, SafeMode.cs: private static SafeMode __true = new SafeMode(WriteConcern.Acknowledged); 回答2: Use the new MongoClient type to connect instead of MongoServer.Create. Write concern will default to acknowledged with MongoClient, which is the equivalent of the obsolete safe mode

Bitwise enum (flags) query using MongoDB's official C# driver

孤者浪人 提交于 2019-12-10 14:41:59
问题 When I try to run a LINQ query of the form: MongoCollection<MyEntity> collection; collection.AsQueryable().Where(entity => (entity.Flags & MyFlags.AFlag) != MyFlags.None); I get an ArgumentException with the message Unsupported where clause: ((Int32)((Int32)entity.Flags & 4) != 0). Is this a known bug/feature? Is there any workaround? From the documentation it seems like MongoDB has a bitwise update, but not a bitwise query. For comparison, the same query runs smoothly above Redis using

MongoDB: How to load collection with nested array in C#?

前提是你 提交于 2019-12-10 13:36:54
问题 I have collection called "servers" with following documents. { name: "West", ip: "123.123.123.123", channels: [ { name: "English", port: "1234", status: "0" }, { name: "Spanish", port: "1235", status: "0" }, { name: "German", port: "1236", status: "0" } ] }, { name: "East", ip: "122.122.122.122", channels: [ { name: "English", port: "1234", status: "0" }, { name: "French", port: "1235", status: "0" } ] } How would I select that from MongoDB using C# using structures? 回答1: If you want all

How do I Moq IFindFluent so this call to ToListAsync works?

*爱你&永不变心* 提交于 2019-12-10 12:43:30
问题 I am unit testing a wrapper to the MongoDB C# driver. I have this line of code: Collection.Find(predicate).ToListAsync(); Where Collection is of type IMongoCollection<T> and Find(predicate) returns an instance implementing IFindFluent<T, T> . ToListAsync() is an extension to turn the results into a list, I assume. I am attempting to write unit tests, and I am stumped on handling this. I can't make a wrapper class because that's what I'm working on. I would prefer to either make it so

MongoDB C# driver 2.0 InsertManyAsync vs BulkWriteAsync

橙三吉。 提交于 2019-12-10 12:34:36
问题 I have to insert many documents in a MongoDB collection, using the new C# 2.0 driver. Is using either collection.InsertManyAsync(...) collection.BulkWriteAsync(...) making any difference? (particularly about performance). From what i understand from MongoDB documentation, an insert with an array of documents should be a bulk operation under the hood. Is that correct? Thanks for your help. 回答1: I found the answer looking at the driver source code: the InsertManyAsync uses internally the

MongoDb Distinct with query C# driver

我的未来我决定 提交于 2019-12-10 10:31:00
问题 I am trying to use the db.collection.distinct(field, query) command, documented here. I am trying to call this with the C# driver, documented here. Currently I am using the code: _repository.Search(item.searchCriteria).Select(i => i.messageId).Distinct().ToList() where messageId is a string and the Search function does: //Create search accross all properties of type. public IQueryable<SearchType> Search(SearchType entity) { Type entityType = entity.GetType(); var propertiesToSearch =

MongoDB C# Driver 'Cursor not found'

人走茶凉 提交于 2019-12-10 04:20:51
问题 I have quite an intensive operation that has a MongoCursor run in a loop for a few hours (on a vb.net app running via the c# driver. I'm not too sure what causes it but I run into an exception after a while Cursor not found This could be because of a cursor timeout, perhaps? Is there a way I can stop it happening? If its a timeout issue how do I place a longer timeout? 回答1: You can disable the cursor's timeout in the C# driver by calling: cursor.SetFlags(QueryFlags.NoCursorTimeout); Otherwise