mongodb-.net-driver

Mongodb — include or exclude certain elements with c# driver

青春壹個敷衍的年華 提交于 2019-12-28 04:56:24
问题 How would I translate this mongo query to a Query.EQ statement in C#? db.users.find({name: 'Bob'}, {'_id': 1}); In other words, I don't want everything returned to C# -- Just the one element I need, the _id. As always, the Mongo C# Driver tutorial is not helpful. 回答1: Update: With new driver version (1.6+) you can avoid fields names hard-coding by using linq instead: var users = usersCollection.FindAllAs<T>() .SetFields(Fields<T>.Include(e => e.Id, e => e.Name)); You can do it via SetFields

MongoDB and Asp Core update only a key: value pair instead of whole model

社会主义新天地 提交于 2019-12-25 18:35:11
问题 In our app we have a user model linked to a mongoDB database. My question is about modifying only a value from key/value pair inside a given user data (of course using the User ID). Here is my simple model: public class User { [BsonId] public string Id { get; set; } public string email { get; set; } = string.Empty; public bool hasAcceptedTerms { get; set; } = false; public int state { get; set; } = 0; public int UserId { get; set; } = 0; public IList<UserFile> SubmittedFiles { get; set; } }

How do I implement this mongodb query & update operation (CSharp driver)?

孤者浪人 提交于 2019-12-25 12:13:49
问题 I have this collection: Books [ { title: Book1, References: [ObjectId(1), ObjectId(3), ObjectId(5)] <- These are object ids of another collection Main-Reference: ObjectId(5) }, { title: Book2, References: [ObjectId(2), ObjectId(5), ObjectId(7)] Main-Reference: ObjectId(5) } { title: Book3, References: [ObjectId(5), ObjectId(7), ObjectId(9)] Main-Reference: ObjectId(7) }, ] I have an operation where I delete a Reference from book collection Example: Assume I have to delete Reference ObjectId(5

How do I implement this mongodb query & update operation (CSharp driver)?

隐身守侯 提交于 2019-12-25 12:13:34
问题 I have this collection: Books [ { title: Book1, References: [ObjectId(1), ObjectId(3), ObjectId(5)] <- These are object ids of another collection Main-Reference: ObjectId(5) }, { title: Book2, References: [ObjectId(2), ObjectId(5), ObjectId(7)] Main-Reference: ObjectId(5) } { title: Book3, References: [ObjectId(5), ObjectId(7), ObjectId(9)] Main-Reference: ObjectId(7) }, ] I have an operation where I delete a Reference from book collection Example: Assume I have to delete Reference ObjectId(5

BsonElement attribute and custom deserialization logic with MongoDB C# driver

巧了我就是萌 提交于 2019-12-25 07:07:23
问题 Consider the following example: public class Foo { private string _text; [BsonElement("text"), BsonRequired] public string Text { get { return _text; } set { _text = value; Bar(_text); } } private void Bar(string text) { //Only relevant when Text is set by the user of the class, //not during deserialization } } The setter of the Text property and, subsequently, the method Bar are called both when the user of the class assigns a new value to the property and during object deserialization by

Using SqlBulkCopy with MongoDB

爱⌒轻易说出口 提交于 2019-12-25 06:21:12
问题 From a previous question, I'm trying to do a SqlBulkCopy from a MongoDB database, and I'm getting an error and can't find what column type I should have: The given value of type ObjectId from the data source cannot be converted to type nvarchar of the specified target column. Where my DataTable Column DataType is MongoDB.Bson.ObjectId . What should be the type in Microsoft Sql Server to host this value? My current code: string connectionString = GetDestinationConnectionString(); var

update in a nested array using C# Driver in MongoDB

南楼画角 提交于 2019-12-25 02:22:32
问题 Here is my exact schema: { "_id" : ObjectId("4fb4fd04b748611ca8da0d45"), "Name" : "Agent name", "City" : "XXXX", "BranchOffice" : [{ "_id" : ObjectId("4fb4fd04b748611ca8da0d46"), "Name" : "Branch name", "City" : "XXXX", "SubBranch" : [{ "_id" : ObjectId("4fb4fd04b748611ca8da0d47"), "Name" : "Sub-Branch Name", "City" : "XXXX" "Users" : [{ "_id" : ObjectId("4fb4fd04b748611ca8da0d48"), "Name" : "User", "City" : "XXXX" }] }] }] } Its Inserted successfully in c#. insert code was below but update

MongoDB aggregate group on inner child collection and get complete document with count

可紊 提交于 2019-12-24 23:59:54
问题 I have a User collection, which further have a 'UserSubscription' collection, which further have 'Subscription > Publication'. Mongo collection looks like this /* 1 */ { "_id" : 1, "UserSubscriptions" : [ { "_id" : 1, "Subscription" : { "_id" : 1, "Publication" : { "_id" : 1, "Code" : "1MM", }, }, { "_id" : 2, "Subscription" : { "_id" : 2, "Publication" : { "_id" : 2, "Code" : "2MM", }, }, { "_id" : 7, "Subscription" : { "_id" : 7, "Publication" : { "_id" : 1, "Code" : "1MM", }, } ] } /* 2 */

MongoDb search performance

给你一囗甜甜゛ 提交于 2019-12-24 20:47:52
问题 I want to know why the follow search in mongo db (C#) would take 50 seconds to execute. I followed the basic idea of http://calv.info/indexing-schemaless-documents-in-mongo/ I have 100,000 records in a collection(captures). On each document I have a SearchTerm Collection public class SearchTerm { public string Key { get; set; } public object Value { get; set; } } public class Capture { //Some other fields public IList<SearchTerm> SearchTerms { get; set; } } I have also defined a index like so

How to delete many documents in a partitioned collection in Azure CosmosDB using MongoDB API

て烟熏妆下的殇ゞ 提交于 2019-12-24 20:43:44
问题 Consider the following document type class Info { public string Id { get; set; } public string UserId { get; set; } // used as partition key public DateTime CreatedAt { get; set; } } I've created a collection using this var bson = new BsonDocument { { "shardCollection", "mydb.userInfo" }, { "key", new BsonDocument(shardKey, "hashed") } }; database.RunCommand(new BsonDocumentCommand<BsonDocument>(bson)); To delete all documents that are older than a certain date, I tried this collection