mongodb-.net-driver

How to find min value in mongodb

与世无争的帅哥 提交于 2019-11-28 04:40:55
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. 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 return the first document, which will be the minimum value for that field. EDIT: note that this is written

MongoDb use filter to match a list

蓝咒 提交于 2019-11-28 02:19:15
I have a list of BsonDocument : var list = db.GetCollection<BsonDocument>(collectionName); var myIds = list.Find(_ => true) .Project(Builders<BsonDocument>.Projection.Include("_id")) .ToList(); that contains: myIds = "{ { "_id" : "cc9d9282-c9d2-4cba-a776-ffddsds274d5" }, { "_id" : "2c1ddd82-c9d2-4dda-afr6-d79ff1274d56" }, { "_id" : "ss969281-c9d2-4cba-a776-d79ffds274d5" } }" And want to query like this: var deleted =list.DeleteMany(Builders<MessageExchange>.Filter.In("_id", myIds)); I also have tried the following: var filter = new BsonDocument("_id", new BsonDocument("$in", new BsonArray

handle @ in mongodb connection string

天大地大妈咪最大 提交于 2019-11-28 00:55:47
I have connection string <add key="MongoDBConnectionString" value="mongodb://user:password@123@127.0.0.1/?safe=true"/> password is password@123 so host is parsing 123@127.0.0.1, how can i handle @ in credential Edit I have tried to escape it but still same problem <add key="MongoDBConnectionString" value="mongodb://user:password@123@127.0.0.1/?safe=true"/> Edit Thanks to alexjamesbrown for helping, following escaping working for me. <add key="MongoDBConnectionString" value="mongodb://user:password%40123@127.0.0.1/?safe=true"/> I posted this as an answer before, but it converted it to a comment

How to use $ positional operator in MongoDB C# driver version 2

半腔热情 提交于 2019-11-28 00:53:35
问题 I need to update a field of one element from array sub document of a document. MongoDB have the $ positional operator to do this. But in MongoDB C# driver version 2 it seems that there is no support for this operator. How can I achieve this? Documents: { "_id" : 1, "grades" : [ 80, 85, 90 ] } { "_id" : 2, "grades" : [ 88, 90, 92 ] } { "_id" : 3, "grades" : [ 85, 100, 90 ] } Expected query: db.students.update( { _id: 1, grades: 80 }, { $set: { "grades.$" : 82 } } ) 回答1: You can try something

Mongodb aggregation query to subtract and grouping of cumulative value

僤鯓⒐⒋嵵緔 提交于 2019-11-28 00:52:10
问题 { "_id" : ObjectId("58f5a22d22679039176d2ee8"), "MachineID" : NumberInt("1001"), "Timestamp" : ISODate("2017-04-18T07:01:01.000+05:30"), "Utilization" : NumberInt("63654480"), "RunStatus" : NumberInt("1"), "ProductsCount" : NumberInt("681350") }, { "_id" : ObjectId("58f5a22d22679039176d2ee9"), "MachineID" : NumberInt("1001"), "Timestamp" : ISODate("2017-04-18T07:02:02.000+05:30"), "Utilization" : NumberInt("63655480"), "RunStatus" : NumberInt("1"), "ProductsCount" : NumberInt("681370") }, { "

C# MongoDB.Driver GetServer is Gone, What Now?

末鹿安然 提交于 2019-11-27 22:21:15
问题 From the mongoDB.Driver docs (http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/) Get a Reference to a Server Object To get a reference to a server object from the client object, write this: var server = client.GetServer(); In the latest release the GetServer method is gone, but the doc have not been updated, what do we use now? Thanks for your time. 回答1: GetServer is part of the old API. To use the new, shiny and async -ready API simply call GetDatabase directly

MongoDB and C#: Case insensitive search

流过昼夜 提交于 2019-11-27 20:24:35
I am using MongoDB and the C# driver for MongoDB . I recently discovered that all queries in MongoDB are case-sensitive. How can I make a case-insensitive search? I found one way to do this: Query.Matches( "FirstName", BsonRegularExpression.Create(new Regex(searchKey,RegexOptions.IgnoreCase))); The simplest and safest way to do that is using Linq : var names = namesCollection.AsQueryable().Where(name => name.FirstName.ToLower().Contains("hamster")); As explained in the tutorial ToLower , ToLowerInvariant , ToUpper and ToUpperInvariant all perform matches in a case insensitive way. After that

MongoDB C# Driver - Ignore fields on binding

微笑、不失礼 提交于 2019-11-27 18:40:32
When using a FindOne() using MongoDB and C#, is there a way to ignore fields not found in the object? EG, example model. public class UserModel { public ObjectId id { get; set; } public string Email { get; set; } } Now we also store a password in the MongoDB collection, but do not want to bind it to out object above. When we do a Get like so, var query = Query<UserModel>.EQ(e => e.Email, model.Email); var entity = usersCollection.FindOne(query); We get the following error Element 'Password' does not match any field or property of class Is there anyway to tell Mongo to ignore fields it cant

Upserting in Mongo DB using official C# driver

被刻印的时光 ゝ 提交于 2019-11-27 18:08:17
In the official documentation of mongodb they mention upserts, so it would be really nice to write an upsert command instead of: if (_campaignRepo.Exists(camp)) { _campaignRepo.DeleteByIdAndSystemId(camp); } _campaignRepo.Save(camp); something which would implement that logic on the db level if it is possible. So what is the way to do an upsert if there is one? Christian Horsdal The following code is from a working app: weekplanStore.Update( Query.EQ("weekNumber", week), Update.Replace(rawWeekPlan), UpdateFlags.Upsert); The weekplanStore is my MongoDB collection, and the code will update the

Mongodb — include or exclude certain elements with c# driver

你。 提交于 2019-11-27 18:00:12
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. Andrew Orsich 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 method of mongodb cursor: var users = usersCollection.FindAllAs<T>() .SetFields("_id") //