mongodb-.net-driver

How to find min value in mongodb

安稳与你 提交于 2019-11-27 05:24:37
问题 How do you do the equivalent of SELECT MIN(Id) AS MinId FROM Table with MongoDB? It looks like I will have to use MapReduce but I can't find any example that shows how to do this. 回答1: You can use a combination of sort and limit to emulate min : > db.foo.insert({a: 1}) > db.foo.insert({a: 2}) > db.foo.insert({a: 3}) > db.foo.find().sort({a: 1}).limit(1) { "_id" : ObjectId("4df8d4a5957c623adae2ab7e"), "a" : 1 } sort({a: 1}) is an ascending (minimum-first) sort on the a field, and we then only

Execute mongodb shell script via C# driver

大憨熊 提交于 2019-11-27 03:28:49
问题 I have read this question and haven't understand. Is there ability to execute arbitrary mongodb shell script via C# driver? 回答1: 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()

MongoDB .NET not generating _id on upsert

烈酒焚心 提交于 2019-11-27 03:17:00
问题 I'm attempting to upsert a document into MongoDB 2.4.4 using the .NET driver. It seems not to automatically generate the _id on upserts, though it does correctly generate the _id on plain inserts. How can I cause the driver to properly generate the _id ? Here is a small example that demonstrates the problem. public class MongoObject { [BsonId(IdGenerator = typeof(StringObjectIdGenerator))] [BsonRepresentation(BsonType.ObjectId)] public string MongoID { get; set; } public int Index { get; set;

Aggregate $lookup with C#

家住魔仙堡 提交于 2019-11-27 02:09:06
I have the following MongoDb query working: db.Entity.aggregate( [ { "$match":{"Id": "12345"} }, { "$lookup": { "from": "OtherCollection", "localField": "otherCollectionId", "foreignField": "Id", "as": "ent" } }, { "$project": { "Name": 1, "Date": 1, "OtherObject": { "$arrayElemAt": [ "$ent", 0 ] } } }, { "$sort": { "OtherObject.Profile.Name": 1 } } ] ) This retrieves a list of objects joined with a matching object from another collection. Does anybody know how I can use this in C# using either LINQ or by using this exact string? I tried using the following code but it can't seem to find the

MongoDB C# Query for 'Like' on string

安稳与你 提交于 2019-11-27 02:06:27
问题 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 回答1: 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") 回答2: For the c# driver 2.1 (MongoDB 3.0) var collection = database.GetCollection<BsonDocument>("<<name of the collection>>"); var filter = Builders

Query MongoDB Using 'ObjectId'

依然范特西╮ 提交于 2019-11-27 02:03: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? 回答1: You need to create an instance of ObjectId and then

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

自古美人都是妖i 提交于 2019-11-27 01:45:20
问题 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 =

How to save date properly?

為{幸葍}努か 提交于 2019-11-27 01:31:53
问题 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? 回答1: 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

MongoDB best practice for referencing

家住魔仙堡 提交于 2019-11-27 01:12:49
问题 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

Mongodb C# driver return only matching sub documents in array

丶灬走出姿态 提交于 2019-11-26 23:39:51
问题 I have a document in this format: { _id: ..., myArray: [{other: stuff}, {other: stuff}, ...], ... } I want to find elements that match certain things, like the _id or fields value from the sub-documents in myArray . I want to return the documents, but with a filtered MyArray where only the matching sub-documents are present. I tried to do a projection and include the matched elements like this: _mongoContext.myDocument .Find(x => x.id == id & x.myArray.Any(y => myList.Contains(t.other)))