close
mongo driver 1.X 轉換到2.0
連線
原本
MongoClient _client = new MongoClient("mongodb://localhost:27017");
MongoServer _server = _client.GetServer();
_db = _server.GetDatabase("DB");
修正MongoClient _client = new MongoClient("mongodb://localhost:27017"); _db = _client.GetDatabase("DB");
查詢
1.數量統計
原本
(int)_db.GetCollection($"device").Count();
修正
(int)_db.GetCollection<object>($"device").Count(new BsonDocument());
2.查詢全部物件
原本
_db.GetCollection<Mng_app>("mng").FindAll().ToList<Mng_app>();
修正
_db.GetCollection<Mng_app>("mng").Find(new BsonDocument()).ToList<Mng_app>();
3.查詢有條件
原本
var res = Query<Mng_app>.EQ(app => app.a, a);
修正
var res = Builders<Mng_app>.Filter.Eq(app => app.a, a);
var list = _db.GetCollection<Mng_app>("mng_app").Find(res).ToList<Mng_app>();
4.查詢有條件2
原本
var list = new BindingList<IMongoQuery>();
list.Add(Query<Notification>.GTE(notification => notification.expire, sendtime));
list.Add(Query<Notification>.LTE(notification => notification.sendtime, sendtime));
list.Add(Query<Notification>.EQ(notification => notification.issend, false));
list.Add(Query<Notification>.EQ(notification => notification.ostype, ostype));
var res = Query.And(list);
修正
var builder = Builders<Notification>.Filter; var list = new BindingList<FilterDefinition<Notification>>(); list.Add(builder.Gte(notification => notification.expire, sendtime)); list.Add(builder.Lte(notification => notification.sendtime, sendtime)); list.Add(builder.Eq(notification => notification.issend, false)); list.Add(builder.Eq(notification => notification.ostype, ostype)); var res = builder.Or(list);
5.查詢條件含有Group、Sort....
原本
var group = new BsonDocument
{
{
{"_id", "$"+v },
{"total", new BsonDocument {{"$sum", 1}}}
}
};
var sort = new BsonDocument { new BsonDocument { { "total", -1 } } } };
var pipeline = new[] { group, sort };
var mongoArgs = new AggregateArgs { Pipeline = pipeline };
var res = _db.GetCollection($"device").Aggregate(mongoArgs).ToList();
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (var x in res)
{
var id = (string)x["_id"];
var total = (int)x["total"];
dic.Add(id, total);
}
修正
var group = new BsonDocument { {"_id", "$"+v }, {"total", new BsonDocument { { "$sum", 1} } } }; var sort = new BsonDocument { { "total", -1 } }; // Fluent and oh yes less code. Tasty var aggregate = _db.GetCollection<Device>($"device").Aggregate().Group(group).Sort(sort); var res = aggregate.ToList(); Dictionary<string, int> dic = new Dictionary<string, int>(); foreach (var x in res) { try { var id = (string)x["_id"]; var total = (int)x["total"]; dic.Add(id, total); } catch (Exception ex) { } }
新增
原本
_db.GetCollection<Mng_app>("mng").Save(app);
修正
_db.GetCollection<Mng_app>("mng").InsertOne(app);
修改
原本
var query = Query.And(
Query<Notification>.EQ(n => n.pushid, pushid),
Query<Notification>.EQ(n => n.msgid, msgid),
Query<Notification>.EQ(n => n.issend, true)
);
var set = Update<Notification>.Set(n => n.isread, true)
.Set(n => n.updatetime, DateTime.Now);
WriteConcernResult result = _db.GetCollection<Notification>($"push").Update(query, set);
修正
var filter = Builders<Notification>.Filter; var query = filter.And( filter.Eq(n => n.pushid, pushid), filter.Eq(n => n.msgid, msgid), filter.Eq(n => n.issend, true) ); var set = Builders<Notification>.Update .Set(n => n.isread, true) .Set(n => n.updatetime, DateTime.Now); var result = _db.GetCollection<Notification>($"push").UpdateMany(query, set);
刪除
原本
var res = Query<Mng_app>.EQ(mng_app => mng_app.appkey, appkey);
var operation = _db.GetCollection<Mng_app>("mng").Remove(res);
修正
var res = Builders<Mng_app>.Filter.Eq(mng_app => mng_app.appkey, appkey); var result = _db.GetCollection<Mng_app>("mng").DeleteMany(res);
全站熱搜
留言列表